批量更新强类型数据集?

时间:2011-03-07 22:09:18

标签: .net asp.net

是否可以在强类型数据集中进行批量更新?创建强类型数据集后,UpdateBatchSize似乎不是一个选项。

1 个答案:

答案 0 :(得分:3)

您是否尝试将此属性添加到DataAdapter?例如,使用Property 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

查看自动生成的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!)'