2011-03-07 117 views

回答

3

您是否尝试将此属性添加到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!)' 
+0

我似乎不具备的UpdateBatchSize我的适配器。这有什么窍门吗? – Carlos

+0

@Carlos:是的,我已经使用UpdateBatchSize属性扩展了自动生成的TableAdapter。我已经在上面描述了它的工作原理。 –

+0

@Tim:我不明白。你的代码不是简单地包装一个叫做UpdateBatchSize的成员吗? – Carlos