<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.Odbc"%>
<%@ Page Language="C#" Debug="true" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
SampleTransaction();
}
void SampleTransaction()
{
System.Data.Odbc.OdbcConnection objConn;
System.Data.Odbc.OdbcCommand objCmd;
String strConnString,strSQL;
OdbcTransaction Trans;
strConnString = "DSN=mydatabase;UID=;PWD=;";
objConn = new System.Data.Odbc.OdbcConnection(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','[email protected]','TH','2000000','1000000')";
objCmd = new System.Data.Odbc.OdbcCommand();
objCmd.Connection = objConn;
objCmd.Transaction = Trans;
objCmd.CommandType = CommandType.Text;
objCmd.CommandText = strSQL;
objCmd.ExecuteNonQuery();
//*** Query 2 ***//
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " +
"VALUES ('C005','Weerachai Nukitram','[email protected]','TH','2000000','1000000')";
objCmd = new System.Data.Odbc.OdbcCommand();
objCmd.Connection = objConn;
objCmd.Transaction = Trans;
objCmd.CommandType = CommandType.Text;
objCmd.CommandText = strSQL;
objCmd.ExecuteNonQuery();
Trans.Commit(); //*** Commit Transaction ***//
this.lblText.Text = "Record is commit";
}
catch (Exception ex)
{
Trans.Rollback(); //*** RollBack Transaction ***//
this.lblText.Text = "Record is rollback ("+ ex.Message +")";
}
objCmd = null;
objConn.Close();
objConn = null;
}
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - System.Data.Odbc</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label id="lblText" runat="Server"></asp:Label>
</form>
</body>
</html>