3
是否可以在強類型數據集中進行批量更新?一旦創建強類型數據集,UpdateBatchSize似乎不是一個選項。批量更新強類型數據集?
是否可以在強類型數據集中進行批量更新?一旦創建強類型數據集,UpdateBatchSize似乎不是一個選項。批量更新強類型數據集?
您是否嘗試將此屬性添加到DataAdapter?擴展自動生成適配器類與屬性的UpdateBatchSize,例如(
沒有測試尚未
):
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
看一看到自動生成designer.cs/vb
你要擴展的命名空間和部分適配器類的名稱使用批量更新功能並將它們放入與數據集名稱相同但沒有designer
的文件中。如果你不能跟着我,看看here。
C#
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!)'
我似乎不具備的UpdateBatchSize我的適配器。這有什麼竅門嗎? – Carlos
@Carlos:是的,我已經使用UpdateBatchSize屬性擴展了自動生成的TableAdapter。我已經在上面描述了它的工作原理。 –
@Tim:我不明白。你的代碼不是簡單地包裝一個叫做UpdateBatchSize的成員嗎? – Carlos