简体   繁体   English

在事务中执行SQL Update批处理

[英]Executing a SQL Update batch within a transaction

  1. I'm loading an excel spreadsheet into a datatable 我正在将Excel电子表格加载到数据表中
  2. Running validation activities against the data to check for issues 针对数据运行验证活动以检查问题
  3. If all validation passes, I will iterate through each row of the datatable and execute an UpdateTable stored procedure with parameters 如果所有验证均通过,我将遍历数据表的每一行并执行带有参数的UpdateTable存储过程
  4. I need to execute this as a transaction b/c I want it to be an all or nothing upload and on error to rollback the transaction 我需要将此作为事务b / c执行,我希望它是全部或不上传,并且在出错时回滚该事务

The problem is that the datatable will have anywhere from 10,000 to 50,000 rows and by cmd.executenonquery firing on every iteration is inefficient and takes longer than it should. 问题在于数据表将具有10,000到50,000行的任何位置,并且通过cmd.executenonquery触发每次迭代效率低下,并且所需时间更长。

 Dim db As New PGS.DBConnection

    Try
        db.BeginTransaction()
        For Each row As DataRow In dt.Rows
            Dim ParameterList(24) As SqlParameter
            ParameterList(0) = New SqlParameter("ProjectID", ProjectID)
            ParameterList(1) = New SqlParameter("Discipline", row("Disc"))
           .
           .
           .    
            ParameterList(24) = New SqlParameter("User", username)

            db.ExecuteQuery("EstimateInsert", ParameterList)

        Next

        db.CommitTransaction()
        Return "success"
    Catch ex As Exception
        db.RollbackTranscation()
        Dim db2 As New PGS.DBConnection
        db2.InsertError(ex.Message, ex.StackTrace, ex.Source, "Excel Estimate Processing", HttpContext.Current.User.Identity.Name)
        Return ex.Message
    End Try

The DBconnection object is handles the using functions to open, execute, and close all things sql. DBconnection对象处理使用函数来打开,执行和关闭所有sql。 In this case giving it a stored procedure name and an array of SQL parameters 在这种情况下,为其提供存储过程名称和SQL参数数组

I don't have to shoe-horn a fix into this framework and if I need to rewrite the entire thing that is fine. 我不必费劲去解决这个框架,如果我需要重写整个事情的话。

Is there a way to run a set a batch size of 500 and speed it up? 有没有一种方法可以运行500个批次的批处理并加快处理速度? Let me know if you need any more code posted...thanks! 让我知道是否需要发布更多代码...谢谢!

错误处理的B / c我不能完全做到这一点,但是我决定使用SQLBULK复制

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

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