Can I do a very large insert with Linq-to-SQL?

前端 未结 4 789
醉酒成梦
醉酒成梦 2021-01-15 01:13

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 (ne         


        
4条回答
  •  礼貌的吻别
    2021-01-15 01:44

    Just for the record I did as marc_s and Peter suggested and chunked the data. It's not especially fast (it took about an hour and a half as Debug configuration, with the debugger attached and quite a lot of console progress output), but it's perfectly adequate for our needs:

    Create a DataContext
    
    numRows = 0;
    While (new data exists)
    {
        Read a record from the text file
    
        Create a new Record 
    
        Populate the record
    
        dataContext.InsertOnSubmit(record)
    
        // Submit the changes in thousand row batches
        if (numRows % 1000 == 999)
            dataContext.SubmitChanges()
    
        numRows++
    }
    
    dataContext.SubmitChanges()
    

提交回复
热议问题