我有一个我写的通用例程,它接受一个 sql 字符串列表并针对数据库执行它们。有什么办法可以使这项工作更快吗?通常,它一次可能会看到 200 次插入、删除或更新。有时会混合使用更新、插入和删除。按类型分隔查询是否是个好主意(即,将插入分组在一起,然后更新然后删除)?
我正在对一个 ms 访问数据库运行它并使用 vb.net 2005。
Public Function ExecuteNonQuery(ByVal sql As List(Of String), ByVal dbConnection as String) As Integer
If sql Is Nothing OrElse sql.Count = 0 Then Return 0
Dim recordCount As Integer = 0
Using connection As New OleDb.OleDbConnection(dbConnection)
connection.Open()
Dim transaction As OleDb.OleDbTransaction = connection.BeginTransaction()
'Using cmd As New OleDb.OleDbCommand()
Using cmd As OleDb.OleDbCommand = connection.CreateCommand
cmd.Connection = connection
cmd.Transaction = transaction
For Each s As String In sql
If Not String.IsNullOrEmpty(s) Then
cmd.CommandText = s
recordCount += cmd.ExecuteNonQuery()
End If
Next
transaction.Commit()
End Using
End Using
Return recordCount
结束功能