简体   繁体   English

使用Linq批量插入Sql(vb.net)

[英]Bulk Insert with Linq to Sql (vb.net)

I'm trying to do a bulk insert into my sql table, using Linq to Sql. 我正在尝试使用Linq to Sql对我的sql表进行批量插入。 I'm still in the learning fase of linq, so please be gentle. 我仍然在学习linq,所以请保持温柔。

The code is not the problem, since everything is importing great, but the process is just terrible slow: 4 seconds for each record, while I have around 15k records. 代码不是问题,因为一切都非常好,但是过程却非常缓慢:每条记录需要4秒,而我有大约15,000条记录。

The main core of the problem (I think) is that before I want to add a record, I first have to check if this record already exist, to be sure I don't have duplicates. 问题的主要核心(我认为)是,在我想要添加记录之前,我首先必须检查该记录是否已经存在,以确保没有重复。 So this means that I do a search into my already existing database of 15k records, and send a true/false response if it has found the record. 因此,这意味着我将对现有的15k条记录数据库进行搜索,并在找到记录后发送true / false响应。

        Dim expr = From spare In db.tblSpareParts Where spare.SparePartDeleted = False Select spare
        For Each part In expr
            If part.SparePartYnumber = ynumber Then
                Return True
            End If
        Next
        Return False

The main code, where we loop through an access document. 主要代码,我们在其中循环访问访问文档。

        For Each Me.dr In dt.Rows
            If dr.Item(0).ToString <> "" Then
                blnfoundit = db.getynumberinfo(dr.Item(0).ToString)
                If blnfoundit Then
                    db.setSparePart("Toevoegen", dr.Item(0).ToString, dr.Item(1).ToString, "", "", "Mat Ref: " & dr.Item(2).ToString & " - Vendor : " & dr.Item(5).ToString, 0, 0, CInt(IIf(dr.Item(12).ToString = "", 0, dr.Item(12).ToString)))
                End If
            End If
        Next

To the end a piece of the code to place everything in SQL: 最后,一段代码将所有内容放入SQL:

                Dim sqlimport As New tblSparePart
                With sqlimport
                    .SparePartCurrentStock = currentstock
                    .SparePartDeleted = False
                    .SparePartDescription = description
                    .SparePartLastModified = DateTime.Now()
                    .SparePartLocation = location
                    .SparePartMinimumStock = minimumstock
                    .SparePartPrice = price
                    .SparePartRemarks = remarks
                    .SparePartType = type
                    .SparePartUserName = General.username
                    .SparePartYnumber = ynumber
                End With
                db.tblSpareParts.InsertOnSubmit(sqlimport)
                db.SubmitChanges()

Any ideas on how to speed up this process? 关于如何加快此过程的任何想法?

Btw, I did search the web for similar things, and found something about SqlBulkCopy, but I have no idea on how to use it, and if it would be useful. 顺便说一句,我确实在网上搜索了类似的内容,并找到了有关SqlBulkCopy的内容,但是我不知道如何使用它以及它是否有用。 I have a Tags Table. 我有一个标签表。 How to Bulk Insert using LINQ? 如何使用LINQ批量插入?

Thanks in advance for the responses, 预先感谢您的回复,

Gert 格特

If this works at all like LINQ to Entities, the slowness here is actually from creating the change tracking objects for each record. 如果这完全像LINQ to Entities一样起作用,那么这里的缓慢实际上是来自为每个记录创建更改跟踪对象。

In that case, you can use DbContext.Configuration.AutoDetectChangesEnabled = False to disable change tracking. 在这种情况下,可以使用DbContext.Configuration.AutoDetectChangesEnabled = False禁用更改跟踪。 Change detection is deferred until the loop is complete and the entire context is processed. 更改检测将推迟到循环完成并处理整个上下文之前。

See this article for more information on the EF side: DbContext AutoDetectChangesEnabled set to false detecting changes 请参阅本文以获取有关EF方面的更多信息: DbContext AutoDetectChangesEnabled设置为false以检测更改

As for your problem with LINQ to SQL, you might look into the DataContext.ObjectTrackingEnabled property as outlined in this article: http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.objecttrackingenabled(v=vs.90).aspx 至于LINQ to SQL的问题,您可以按照本文概述的方法来研究DataContext.ObjectTrackingEnabled属性: http : //msdn.microsoft.com/zh-cn/library/system.data.linq.datacontext.objecttrackingenabled( v = VS.90)的.aspx

Perhaps setting this property to false before your loop and back to true afterwards (but before SubmitChanges) might help performance. 也许在循环之前将此属性设置为false,然后在之后(但在SubmitChanges之前)将该属性设置为true可能会提高性能。

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

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