我可以做LINQ到SQL一个非常大的插入? [英] Can I do a very large insert with Linq-to-SQL?
问题描述
我已经得到了我加载到一个SQL Server使用2005数据库的一些文本数据LINQ到SQL使用这种方法(伪代码):
I've got some text data that I'm loading into a SQL Server 2005 database using Linq-to-SQL using this method (psuedo-code):
Create a DataContext
While (new data exists)
{
Read a record from the text file
Create a new Record
Populate the record
dataContext.InsertOnSubmit(record);
}
dataContext.SubmitChanges();
中的代码是一个小C#控制台应用程序。这工作得很好,到目前为止,但我对做真正的数据导入(而不是测试子集),这包含约200万行,而不是我测试过的1000。难道我将不得不做一些巧妙的配料或类似,以避免代码翻倒或执行可悲的事情,还是应该LINQ到SQL处理这个优雅?
The code is a little C# console application. This works fine so far, but I'm about to do an import of the real data (rather than a test subset) and this contains about 2 million rows instead of the 1000 I've tested. Am I going to have to do some clever batching or something similar to avoid the code falling over or performing woefully, or should Linq-to-SQL handle this gracefully?
推荐答案
它看起来像这将然而工作由DataContext的不断变化(因此内存)会与每个InsertOnSubmit增长。也许这是推荐用来执行的SubmitChanges每100个记录?
It looks like this would work however the changes (and thus memory) that are kept by the DataContext are going to grow with each InsertOnSubmit. Maybe it's adviseable to perform a SubmitChanges every 100 records?
我也想看看的 SqlBulkCopy的来看看它不适合您的用例更好。
I would also take a look at SqlBulkCopy to see if it doesn't fit your usecase better.
这篇关于我可以做LINQ到SQL一个非常大的插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!