您应该的SqlDataAdapter的UpdateBatchSize属性设置为0(无限制)。 我没有看到更新table2而没有循环table1的方法。
这里是一个示例代码向您展示实现这一目标的一种方法:
Public Sub BatchUpdate(ByVal table1 As DataTable)
Dim connectionStringServer2 As String = GetConnectionString()
Using connection As New SqlConnection(connectionStringServer2)
Dim adapter As New SqlDataAdapter()
'Set the UPDATE command and parameters'
adapter.UpdateCommand = New SqlCommand(_
"UPDATE Table2 SET " _
& "[email protected],[email protected] WHERE [email protected];", _
connection)
adapter.UpdateCommand.Parameters.Add("@Name", _
SqlDbType.NVarChar, 50, "Name")
adapter.UpdateCommand.Parameters.Add("@Date", _
SqlDbType.DateTime, 0, "Date")
adapter.UpdateCommand.Parameters.Add("@TableOneId", _
SqlDbType.Int, 0, "TableOneId")
adapter.UpdateCommand.UpdatedRowSource = _
UpdateRowSource.None
' Set the batch size,'
' try to update all rows in a single round-trip to the server'
adapter.UpdateBatchSize = 0
Dim table2 As New DataTable("table2")
table2.Columns.Add(New DataColumn("Name", GetType(String)))
table2.Columns.Add(New DataColumn("Date", GetType(Date)))
table2.Columns.Add(New DataColumn("TableOneId", GetType(Int32)))
' copy content from table1 to table2'
For Each row As DataRow In table1.Rows
Dim newRow = table2.NewRow
newRow("TableOneId") = row("ID")
newRow("Name") = row("Name")
newRow("Date") = row("Date")
table2.Rows.Add(newRow)
' note: i have not tested following, but it might work or give you a clue'
newRow.AcceptChanges()
newRow.SetModified()
Next
' Execute the update'
adapter.Update(table2)
End Using
End Sub
你想避免循环DataTable或避免往返数据库? –
我想使用BatchUpdate,因为性能问题是正确的。 –
好的,我的代码工作吗? –