Slee
Slee

Reputation: 28248

bulk update strongly typed dataset?

is it possible to do a batch update in a strongly typed data set? UpdateBatchSize does not seem to be an option once you create a strongly typed dataset.

Upvotes: 3

Views: 1487

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460138

Have you tried to add this property to the DataAdapter? Extend the autogenerated Adapter class with a Property UpdateBatchSize, for example(not tested yet):

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

Have a look into the autogenerated designer.cs/vb for the names of the Namespace and the partial adapter-classes you want to extend with the bulk-update-functionality and put them into a file with the same name as the Dataset but without designer. If you cannot follow me, have a look 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;
                }
            }
        }
    }
}

If that works , it only works the way i described, because you cannot change the autogenerated file directly, because it will be regenerated automatically on changes.

Edit: After reading this i changed the code above to set the UpdatedRowSource property accordingly.

Tested with following code:

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!)'

Upvotes: 3

Related Questions