Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

UpdateDataAdapter - Update a data source through a DataAdapter

Total Hit ( 2090)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Click here to copy the following block
' 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 failed

Function 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 affectedRows
End Function


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.