简体   繁体   English

如何使用Linq-to-SQL将一个表中的所有行插入到另一个表中?

[英]How can I insert all rows from one table into another table with Linq-to-SQL?

I have two tables in SQL Server. 我在SQL Server中有两个表。

Table one contains these columns: 表一包含以下各列:

1-id
2-name
3-family
4-address

and table two contains these columns: 表二包含这些列:

1-id
2-name

In table one I have 100000 rows and read all record with this query: 在表一中,我有100000行,并使用此查询读取所有记录:

var query = (from p in datacontext.table1
             select p).toArray();

I want insert all data from up query into the table2, now I use this method: 我想将所有来自向上查询的数据插入到table2中,现在我使用此方法:

for(int i = 0; i < query.count(); i++) {
     table2 beh = new tabl2();
     beh.name = query[0].name;
     datacontext.table2.insertonsubmit(beh);
     datacontext.submitchange();
}

Is there another way? 还有另一种方法吗? Thanks. 谢谢。

Making use of Linq to SQL to insert record one by one will take lot of time. 利用Linq to SQL一条一条插入记录将花费大量时间。 Instead of that I Suggest make use of Bulk insert so that your data get insert in one go in less amount of time for that you can make use of of DataTable and OpenXML. 与其相反,我建议您使用批量插入,这样您就可以一次使用DataTable和OpenXML来一次性插入数据。 for that tutorial is : Bulk Insertion of Data Using C# DataTable and SQL server OpenXML function 该教程的内容是: 使用C#DataTable和SQL Server OpenXML函数批量插入数据

or use this 或使用这个

SqlBulkCopy.WriteToServer Method (DataTable) SqlBulkCopy.WriteToServer方法(数据表)

Try 尝试

var bulkCopy = new SqlBulkCopy(connection);
bulkCopy.DestinationTableName = "table2";
bulkCopy.ColumnMappings.Add("Name", "Name");

using (var reader = new EntityDataReader<Table1>(query))
{
    bulkCopy.WriteToServer(reader);
}

EntityDataReader EntityDataReader

Using Bulk insert in System.Data.SqlClient data get insert in to db in less amount of time using the help of datatable System.Data.SqlClient使用批量插入可借助datatable在更少的时间内将插入到db中

 DataTable dt = getData();
 SqlBulkCopyOptions options = SqlBulkCopyOptions.Default;
                    using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(sqlconnection, options, null))
                    {
                        dt.Columns.Add("Id", typeof(long)).SetOrdinal(0);
                        dt = AddDefaultColumn(dt);
                        sqlBulkCopy.BulkCopyTimeout = 300;
                        sqlBulkCopy.DestinationTableName = "tableName";
                        sqlBulkCopy.WriteToServer(dt);
                    }

Say your table name is table1 and table2 with the columns of id and description you can use 假设您的表名是table1和table2,其中包含ID和description列,您可以使用

INSERT INTO table2  (id, description)
       SELECT table2.id, table2.description
       FROM table1;

furthermore you can add a where 此外,您可以在其中添加

INSERT INTO table2  (id, description)
           SELECT table2.id, table2.description
           FROM table1 where table1.id =1;

you can visit this link for more info https://technet.microsoft.com/en-us/library/ms188263%28v=sql.105%29.aspx 您可以访问此链接以获取更多信息https://technet.microsoft.com/zh-cn/library/ms188263%28v=sql.105%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM