如何循环以通过从两(2)个其他表中获取数据将记录插入表中?

时间:2022-05-15 11:59:59

I have 3 tables: tblDepartment, tblFunction, tblDepartmentFunction as below. tblDepartment having one to many with tblDepartmentFunction tblFunction having one to many with tblDepartmentFunction I need code C# to run a loop to insert the records to tblDepartmentFunction by taking DepartmentID (1) record from tblDepartment together taking FunctionID (1,2,3,4,5,6) of all records from tblFunction please refer the below drawing:

我有3个表:tblDepartment,tblFunction,tblDepartmentFunction如下。 tblDepartment有一对多的tblDepartmentFunction tblFunction与tblDepartmentFunction有一对多我需要代码C#来运行一个循环,将记录插入tblDepartmentFunction,从tblDepartment获取DepartmentID(1)记录一起取FunctionID(1,2,3,4,5) ,6)来自tblFunction的所有记录请参考下图:

如何循环以通过从两(2)个其他表中获取数据将记录插入表中?

Please help me to obtain the above.

请帮助我获得上述内容。

1 个解决方案

#1


-1  

You can feed an insert command with a query, and that query can join two or more tables. So you don't need to code a loop in C#, you can just insert those rows executing a single SQL statement.

您可以使用查询提供insert命令,该查询可以连接两个或多个表。因此,您不需要在C#中编写循环代码,只需插入执行单个SQL语句的行即可。

Concretely :

insert into tblDepartmentFuncion (DepartmentId, FunctionId)
            select DepartmentId, FunctionId
            from tblDepartment, tblFunction

#1


-1  

You can feed an insert command with a query, and that query can join two or more tables. So you don't need to code a loop in C#, you can just insert those rows executing a single SQL statement.

您可以使用查询提供insert命令,该查询可以连接两个或多个表。因此,您不需要在C#中编写循环代码,只需插入执行单个SQL语句的行即可。

Concretely :

insert into tblDepartmentFuncion (DepartmentId, FunctionId)
            select DepartmentId, FunctionId
            from tblDepartment, tblFunction