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
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if the program quits, the database will rollback the query on it's side.
to rollback explicitely, simply use the conn.Rollback Method.
to rollback explicitely, simply use the conn.Rollback Method.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
gr888 .. my first "earned" expert points
ASKER
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?