UpdateDataAdapter – Update a data source through a DataAdapter

UpdateDataAdapter – Update a data source through a DataAdapter

' update a data source through a DataAdapter' ' DA is the OleDbDataAdapter or SqlDataAdapter object' DT is the DataTable to be updated' if USETRANSACTIONS is True, all rows are updated, or none'' returns the number of affected records' or -1 if the update failedFunction UpdateDataAdapter(ByVal da As System.Data.IDbDataAdapter, _    ByVal dt As DataTable, ByVal useTransactions As Boolean) As Integer    Dim tr As System.Data.IDbTransaction    Dim affectedRows As Integer    ' we need a reference to a concrete DbDataAdapter object    Dim dda As System.Data.Common.DbDataAdapter = DirectCast(da, _        System.Data.Common.DbDataAdapter)    If useTransactions Then        ' get the connection object related to the DataAdapter        Dim cn As System.Data.IDbConnection = da.SelectCommand.Connection        ' open a transaction on that connection        tr = da.SelectCommand.Connection.BeginTransaction()        ' enroll all commands in that transaction        da.DeleteCommand.Transaction = tr        da.InsertCommand.Transaction = tr        da.UpdateCommand.Transaction = tr        ' we need an exception if an update conflict occurs        dda.ContinueUpdateOnError = False    Else        ' otherwise just ignore conflicts        dda.ContinueUpdateOnError = True    End If    Try        ' perform the update        affectedRows = dda.Update(dt)        ' if we get here, we can commit the transaction (if there is one)        If Not (tr Is Nothing) Then tr.Commit()    Catch ex As Exception        ' in this case we must rollback the transaction         ' (if there is one) and swallow the exception        If Not (tr Is Nothing) Then tr.Rollback()        affectedRows = -1    End Try    ' return the number of affected rows    Return affectedRowsEnd Function

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of