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:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS