在强类型数据集中是否可能进行批量更新?
一旦创建了强类型数据集,似乎UpdateBatchSize不再是一个选项。
一旦创建了强类型数据集,似乎UpdateBatchSize不再是一个选项。
您是否尝试将此属性添加到DataAdapter中?例如,使用属性UpdateBatchSize扩展自动生成的Adapter类(尚未测试):
Namespace DataSet1TableAdapters
Partial Public Class AddressTableAdapter
Public Property UpdateBatchSize() As Integer
Get
Return Me.Adapter.UpdateBatchSize
End Get
Set(ByVal value As Integer)
Me.Adapter.UpdateBatchSize = value
If value <> 1 Then
Me.Adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.None
Me.Adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None
Me.Adapter.DeleteCommand.UpdatedRowSource = UpdateRowSource.None
Else
Me.Adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord
Me.Adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord
Me.Adapter.DeleteCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord
End If
End Set
End Property
End Class
End Namespace
namespace DataSet1TableAdapters
{
public partial class AddressTableAdapter
{
public int UpdateBatchSize {
get { return this.Adapter.UpdateBatchSize; }
set {
this.Adapter.UpdateBatchSize = value;
if (value != 1) {
this.Adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;
this.Adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None;
this.Adapter.DeleteCommand.UpdatedRowSource = UpdateRowSource.None;
} else {
this.Adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;
this.Adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;
this.Adapter.DeleteCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;
}
}
}
}
}
<如果这样可以的话>,它只能按照我描述的方式工作,因为您无法直接更改自动生成的文件,因为它会在更改时自动重新生成。
编辑:阅读this后,我更改了上面的代码以相应地设置UpdatedRowSource
属性。
使用以下代码进行测试:
Dim stopWatch As New Stopwatch
Dim da As New DataSet1TableAdapters.AddressTableAdapter
Dim tblAllAdresses As New DataSet1.AddressDataTable
Dim tsBS1, tsBS0 As TimeSpan
da.Fill(tblAllAdresses)
da.UpdateBatchSize = 1
For Each adrr As DataSet1.AddressRow In tblAllAdresses
adrr.ModifiedDate = Date.Now
Next
stopWatch.Start()
Dim addressesChanged As Int32 = da.Update(tblAllAdresses)
stopWatch.Stop()
tsBS1 = stopWatch.Elapsed
da.UpdateBatchSize = 0 '0 means maximum server can handle'
For Each adrr As DataSet1.AddressRow In tblAllAdresses
adrr.ModifiedDate = Date.Now
Next
stopWatch.Restart()
addressesChanged = da.Update(tblAllAdresses)
stopWatch.Stop()
tsBS0 = stopWatch.Elapsed
Console.WriteLine("tsBS1: " & tsBS1.Minutes & ":" & tsBS1.Seconds & ":" & tsBS1.Milliseconds) '12 seconds'
Console.WriteLine("tsBS0: " & tsBS0.Minutes & ":" & tsBS0.Seconds & ":" & tsBS0.Milliseconds) '9 seconds(on localhost!)'