我在另一个 SQL 服务器上有一个表,我需要在一夜之间从中复制。目标表的结构非常相似,所以我只是打算使用类似下面的代码。 来源 - http://forums.asp.net/t/1322979.aspx/1
我还没有尝试过,但是在 linq 中有更好/更快的方法吗?
//there exist two table list and listSecond
DataClassesDataContext dataClass = new DataClassesDataContext(); //create the instance of the DataContext
var str = from a in dataClass.lists select a;
foreach (var val in str) // iterator the data from the list and insert them into the listSecond
{
listSecond ls = new listSecond();
ls.ID = val.ID;
ls.pid = val.pid;
ls.url = val.url;
dataClass.listSeconds.InsertOnSubmit(ls);
}
dataClass.SubmitChanges();
Response.Write("success");
最佳答案
使用 LINQ 插入大量数据不是一个好主意,除非使用在复制之前需要大量转换的复杂架构。除了将它们全部记录在事务日志中之外,它将为插入的每一行创建一个单独的查询。
可以找到更快的解决方案 here - 它使用 SqlBulkCopy
,这是一种在单个查询中插入大量数据的方法,没有事务日志记录来减慢速度。它的速度将一个数量级,我是根据这两种方法的个人经验告诉你的。
关于c# - 使用linq将数据从一个表复制到另一个表的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8940935/