如何使用Linq-to-SQL将一个表中的所有行插入到另一个表中? [英] How can I insert all rows from one table into another table with Linq-to-SQL?

查看:51
本文介绍了如何使用Linq-to-SQL将一个表中的所有行插入到另一个表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQL Server中有两个表.

I have two tables in SQL Server.

表一包含以下各列:

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

表2包含以下列:

1-id
2-name

在表1中,我有100000行,并通过此查询读取所有记录:

In table one I have 100000 rows and read all record with this query:

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

我想将向上查询中的所有数据插入到table2中,现在我使用此方法:

I want insert all data from up query into the table2, now I use this method:

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.

推荐答案

在SQL上使用Linq到SQL插入记录将花费大量时间.与其相反,我建议使用大容量插入,这样您就可以一次使用DataTable和OpenXML来一次性插入数据.该教程的内容是:批量插入数据使用C#DataTable和SQL Server OpenXML函数

Making use of Linq to SQL to insert record one by one will take lot of time. 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. for that tutorial is : Bulk Insertion of Data Using C# DataTable and SQL server OpenXML function

或使用

SqlBulkCopy.WriteToServer方法(数据表)

这篇关于如何使用Linq-to-SQL将一个表中的所有行插入到另一个表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆