简体   繁体   English

使用LINQ将大量数据插入数据库

[英]Insert large amount of data into database using LINQ

I want to insert around 1 million records into a database using Linq in ASP.NET MVC. 我想在ASP.NET MVC中使用Linq将大约100万条记录插入到数据库中。 But when I try the following code it didn't work. 但是,当我尝试以下代码时,它不起作用。 It's throwing an OutOfMemoryException . 它正在抛出OutOfMemoryException And also it took 3 days in the loop. 而且它在循环中花了3天。 Can anyone please help me on this??? 任何人都可以帮我这个???

db.Database.ExecuteSqlCommand("DELETE From [HotelServices]");

DataTable tblRepeatService = new DataTable();
tblRepeatService.Columns.Add("HotelCode",typeof(System.String));
tblRepeatService.Columns.Add("Service",typeof(System.String));
tblRepeatService.Columns.Add("Category",typeof(System.String));

foreach (DataRow row in xmltable.Rows)
{
     string[] servicesarr = Regex.Split(row["PAmenities"].ToString(), ";");

     for (int a = 0; a < servicesarr.Length; a++)
     {
         tblRepeatService.Rows.Add(row["HotelCode"].ToString(), servicesarr[a], "PA");
     }

     String[] servicesarrA = Regex.Split(row["RAmenities"].ToString(), ";");

     for (int b = 0; b < servicesarrA.Length; b++)
     {
         tblRepeatService.Rows.Add(row["hotelcode"].ToString(), servicesarrA[b], "RA");
     }
}

HotelAmenties _hotelamenties;

foreach (DataRow hadr in tblRepeatService.Rows)
{
     _hotelamenties = new HotelAmenties();
     _hotelamenties.Id = Guid.NewGuid();
     _hotelamenties.ServiceName = hadr["Service"].ToString();
     _hotelamenties.HotelCode = hadr["HotelCode"].ToString();

     db.HotelAmenties.Add(_hotelamenties);
}

db.SaveChanges();

tblRepeatService table has around 1 million rows. tblRepeatService表有大约100万行。

Bulk inserts like this are highly inefficient in LINQtoSQL. 像这样的批量插入在LINQtoSQL中非常低效。 Every insert creates at least three objects (the DataRow , the HotelAmenities object and the tracking record for it), chewing up memory on objects you don't need. 每个插件都会创建至少三个对象( DataRowHotelAmenities对象及其跟踪记录),从而在不需要的对象上占用内存。

Given that you already have a DataTable , you can use System.Data.SqlClient.SqlBulkCopy to push the content of the table to a temporary table on the SQL server, then use a single insert statement to load the data into its final destination. 鉴于您已经拥有DataTable ,可以使用System.Data.SqlClient.SqlBulkCopy将表的内容推送到SQL服务器上的临时表,然后使用单个insert语句将数据加载到其最终目标中。 This is the fastest way I have found so far to move many thousands of records from memory to SQL. 这是迄今为止我发现的将数千条记录从内存迁移到SQL的最快方法。

If performance doesn't matter and this is a 1 shot job you can stick to the way you're using. 如果表现无关紧要,这是一次性工作,你可以坚持你的使用方式。 Your problem is you're only saving at the end, so entity Framework has to store and generate the SQL for 1 million operations at once, modify your code so that you save every 1000 or so inserts instead of only at the end and it should work just fine. 你的问题是你最后只保存,所以实体框架必须同时存储和生成100万个操作的SQL,修改你的代码,这样你就可以保存每1000个左右的插入而不仅仅是在结尾,它应该工作得很好。

int i = 0;
foreach (DataRow hadr in tblRepeatService.Rows)
{ 
     _hotelamenties = new HotelAmenties();
     _hotelamenties.Id = Guid.NewGuid();
     _hotelamenties.ServiceName = hadr["Service"].ToString();
     _hotelamenties.HotelCode = hadr["HotelCode"].ToString();

     db.HotelAmenties.Add(_hotelamenties);
     if((i%1000)==0){
     db.SaveChanges();
     }
     i++;
}    
db.SaveChanges();

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

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