web 2.0

VB.NET & Transaction()

VB.NET & Transaction() The transaction makes sure that changes that were made to the store are treated as a group that can be committed or rolled back.

ShotDev Focus:
- VB.NET & Transaction()

Example

Transaction.aspx

<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%@ Page Language="VB" %>
<script runat="server">

Dim objConn As System.Data.OleDb.OleDbConnection
Dim objCmd As System.Data.OleDb.OleDbCommand
Dim strConnString,strSQL As String
Dim Trans As OleDbTransaction

strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database/mydatabase.mdb")&";Jet OLEDB:Database Password=;"
objConn = New System.Data.OleDb.OleDbConnection(strConnString)
objConn.Open()

'*** Start Transaction ***'
Trans = objConn.BeginTransaction(IsolationLevel.ReadCommitted)

Try

'*** Query 1 ***'
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " & _
"VALUES ('C005','Weerachai Nukitram','webmaster@thaicreate.com','TH','2000000','1000000')"
objCmd = New System.Data.OleDb.OleDbCommand()
With objCmd
.Connection = objConn
.Transaction = Trans
.CommandType = CommandType.Text
.CommandText = strSQL
End With
objCmd.ExecuteNonQuery()

'*** Query 2 ***'
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " & _
"VALUES ('C005','Weerachai Nukitram','webmaster@thaicreate.com','TH','2000000','1000000')"
objCmd = New System.Data.OleDb.OleDbCommand()
With objCmd
.Connection = objConn
.Transaction = Trans
.CommandType = CommandType.Text
.CommandText = strSQL
End With
objCmd.ExecuteNonQuery()

Trans.Commit() '*** Commit Transaction ***'
Me.lblText.Text = "Record is commit"

Catch ex As Exception
Trans.Rollback() '*** RollBack Transaction ***'

Me.lblText.Text = "Record is rollback ("& ex.Message &")"
End Try

objCmd = Nothing
objConn.Close()
objConn = Nothing

</script>

.
.
.
Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (1 votes, average: 1.00 out of 10)
Loading ... Loading ...

Leave a Reply

You must be logged in to post a comment.