Link to home
Start Free TrialLog in
Avatar of nwn123
nwn123

asked on

How to apply a Transaction and Rollback in VB.Net ?

Hi All Experts,

I am using the following code to store(insert / update SQL statement) a data to MySQL 4.1 Server. I want to know is it possible to add the "Transaction" and "Rollback" functions to my situtation.

My Code here:
    Public Function setSQLcommandAdapter(ByVal connection_str As String, ByVal SQL_str As String) As Boolean
        Dim conn As New MySqlConnection
        Dim myCommand As New MySqlCommand

        conn.ConnectionString = connection_str
        Try
            conn.Open()
            Try
                myCommand.Connection = conn
                myCommand.CommandText = SQL_str
                myCommand.ExecuteNonQuery()
                conn.Close()
                conn.Dispose()
                Return True
            Catch myerror As MySqlException
                MsgBox("There was an error reading from the database: " & myerror.Message)
                Return False
            End Try
        Catch myerror As MySqlException
            MessageBox.Show("Error connecting to the database: " & myerror.Message)
            Return False
        Finally
            If conn.State <> ConnectionState.Closed Then conn.Close()
        End Try
    End Function
SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nwn123
nwn123

ASKER

Thankyou for your comment, angelIII.
Can you explain more for the following code relation?
  line1:             myCommand.ExecuteNonQuery()
  line2:             conn.Commit()
If line1 executed, but the program abnormal quit(line2 no run). What happend is it?
And how can I rollback the sql statement?
if the program quits, the database will rollback the query on it's side.
to rollback explicitely, simply use the conn.Rollback Method.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
gr888 .. my first "earned" expert points