C#中使用OracleTransaction
1、 OracleTransaction类的概述(from msdn )
应用程序通过针对OracleConnection对象调用 BeginTransaction 来创建OracleTransaction对象。对OracleTransaction对象执行与该事务关联的所有后续操作(例如提交或中止该事务)。
示例
OracleTransaction的成员主要有:
属性:
Connection,指定与该事务关联的OracleConnection对象;
IsolationLevel,指定该事务的IsolationLevel;枚举类型,用于对事物的锁定,取值有Chaos、ReadCommited、 ReadUncommited、RepeatableRead、Serializable、Unspecified。
方法:
Commit,提交SQL数据库事务;
Rollback , 从挂起状态回滚事务;
下面的示例创建一个 OracleConnection 和一个 OracleTransaction。它还演示了如何使用 BeginTransaction、Commit 和 Rollback 方法。
[C#]
public void RunOracleTransaction(string myConnString)
{
OracleConnection myConnection = new OracleConnection(myConnString);
myConnection.Open();
OracleCommand myCommand = myConnection.CreateCommand();
OracleTransaction myTrans;
OracleTransaction myTrans;
// Start a local transaction
myTrans = myConnection.BeginTransaction(IsolationLevel.ReadCommitted);
// Assign transaction object for a pending local transaction
myCommand.Transaction = myTrans;
myTrans = myConnection.BeginTransaction(IsolationLevel.ReadCommitted);
// Assign transaction object for a pending local transaction
myCommand.Transaction = myTrans;
try
{
myCommand.CommandText = "INSERT INTO Dept (DeptNo, Dname, Loc) values (50, 'TECHNOLOGY', 'DENVER')";
myCommand.ExecuteNonQuery();
myCommand.CommandText = "INSERT INTO Dept (DeptNo, Dname, Loc) values (60, 'ENGINEERING', 'KANSAS CITY')";
myCommand.ExecuteNonQuery();
myTrans.Commit();
Console.WriteLine("Both records are written to database.");
}
catch(Exception e)
{
myTrans.Rollback();
Console.WriteLine(e.ToString());
Console.WriteLine("Neither record was written to database.");
}
finally
{
myConnection.Close();
}
}