Wednesday, March 7, 2012

how to commit/rollback

i am using oledb to connect the data server, how to use commit and rollback, I am really newbie, could you be so kind to show me a sample code.
thanks.First :
set implicit_transactions on
then run update / insert
run select to check if OK
If OK, commit
if not, rollback
then you can turn this off again:
set implicit_transactions off

Remember udpates / inserts that are not commited can cause the DB to be blocked / slow for other users.

Hope this helps
HandyMac|||my code (enable commit/rollback) as follow:
But HOW can I commit/rollback in other function.
I mean,do not execute commit/rollback this the exec sql function, in the other function,(let the user chooose,the user can choose rollback even the result is correct).
Thanks for help.
Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(con_str)
Dim trans As OleDb.OleDbTransaction
Dim cmd As OleDb.OleDbCommand = conn.CreateCommand
Dim da As OleDb.OleDbDataAdapter
Dim ds As DataSet
Dim dt As DataTable
'open
Try
conn.Open()
Catch ex As OleDb.OleDbException
MsgBox(ex.Message)
Exit Function
End Try
'
trans = conn.BeginTransaction(IsolationLevel.ReadCommitted )
cmd.Connection = conn
cmd.Transaction = trans
cmd.CommandText = "select * from products"
'exec sql
Dim rolled As Boolean = False
Try
da = New OleDb.OleDbDataAdapter(cmd.CommandText, conn)
cmd.ExecuteNonQuery()
trans.Commit()
MsgBox("commited")
Catch ex As Exception
MsgBox(ex.Message)
trans.Rollback()
MsgBox("rollback")
rolled = True
End Try
If rolled Then
Else
'fill ds
dt = New DataTable("wa")
da.Fill(dt)
grd.DataSource = dt.DefaultView
End If

'closing

conn.Close()
'
Return True

No comments:

Post a Comment