OracleConnection.BeginTransaction 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
開始進行資料庫上的交易。
多載
BeginTransaction() |
開始進行資料庫上的交易。 |
BeginTransaction(IsolationLevel) |
使用指定的 IsolationLevel 值,開始進行資料庫上的交易。 |
BeginTransaction()
開始進行資料庫上的交易。
public:
System::Data::OracleClient::OracleTransaction ^ BeginTransaction();
public System.Data.OracleClient.OracleTransaction BeginTransaction ();
member this.BeginTransaction : unit -> System.Data.OracleClient.OracleTransaction
override this.BeginTransaction : unit -> System.Data.OracleClient.OracleTransaction
Public Function BeginTransaction () As OracleTransaction
傳回
代表新異動的物件。
例外狀況
不支援平行交易。
範例
下列範例會 OracleConnection 建立 和 OracleTransaction。 它也會示範如何使用 BeginTransaction、 Commit和 Rollback 方法。
public void RunOracleTransaction(string connectionString)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
connection.Open();
OracleCommand command = connection.CreateCommand();
OracleTransaction transaction;
// Start a local transaction
transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
// Assign transaction object for a pending local transaction
command.Transaction = transaction;
try
{
command.CommandText =
"INSERT INTO Dept (DeptNo, Dname, Loc) values (50, 'TECHNOLOGY', 'DENVER')";
command.ExecuteNonQuery();
command.CommandText =
"INSERT INTO Dept (DeptNo, Dname, Loc) values (60, 'ENGINEERING', 'KANSAS CITY')";
command.ExecuteNonQuery();
transaction.Commit();
Console.WriteLine("Both records are written to database.");
}
catch (Exception e)
{
transaction.Rollback();
Console.WriteLine(e.ToString());
Console.WriteLine("Neither record was written to database.");
}
}
}
Public Sub RunOracleTransaction(ByVal connectionString As String)
Using connection As New OracleConnection(connectionString)
connection.Open()
Dim command As OracleCommand = connection.CreateCommand()
Dim transaction As OracleTransaction
' Start a local transaction
transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)
' Assign transaction object for a pending local transaction
command.Transaction = transaction
Try
command.CommandText = _
"INSERT INTO Dept (DeptNo, Dname, Loc) values (50, 'TECHNOLOGY', 'DENVER')"
command.ExecuteNonQuery()
command.CommandText = _
"INSERT INTO Dept (DeptNo, Dname, Loc) values (60, 'ENGINEERING', 'KANSAS CITY')"
command.ExecuteNonQuery()
transaction.Commit()
Console.WriteLine("Both records are written to database.")
Catch e As Exception
transaction.Rollback()
Console.WriteLine(e.ToString())
Console.WriteLine("Neither record was written to database.")
End Try
End Using
End Sub
備註
若要認可或回復交易,您必須明確使用 Commit 或 Rollback 方法。
若要確保適用於 Oracle 事務管理模型的數據提供者 .NET Framework 正確執行,請避免使用其他交易管理模型,例如資料庫所提供的模型。
注意
如果您未指定隔離等級,則會使用預設隔離等級。 若要使用 BeginTransaction 方法指定隔離等級,請使用 BeginTransaction 多載。
另請參閱
適用於
BeginTransaction(IsolationLevel)
使用指定的 IsolationLevel 值,開始進行資料庫上的交易。
public:
System::Data::OracleClient::OracleTransaction ^ BeginTransaction(System::Data::IsolationLevel il);
public System.Data.OracleClient.OracleTransaction BeginTransaction (System.Data.IsolationLevel il);
member this.BeginTransaction : System.Data.IsolationLevel -> System.Data.OracleClient.OracleTransaction
override this.BeginTransaction : System.Data.IsolationLevel -> System.Data.OracleClient.OracleTransaction
Public Function BeginTransaction (il As IsolationLevel) As OracleTransaction
參數
這個連接的交易隔離等級 (Isolation Level)。
傳回
代表新異動的物件。
例外狀況
不支援平行交易。
範例
下列範例會 OracleConnection 建立 和 OracleTransaction。 它也會示範如何使用 BeginTransaction、 Commit和 Rollback 方法。
public void RunOracleTransaction(string connectionString)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
connection.Open();
OracleCommand command = connection.CreateCommand();
OracleTransaction transaction;
// Start a local transaction
transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
// Assign transaction object for a pending local transaction
command.Transaction = transaction;
try
{
command.CommandText =
"INSERT INTO Dept (DeptNo, Dname, Loc) values (50, 'TECHNOLOGY', 'DENVER')";
command.ExecuteNonQuery();
command.CommandText =
"INSERT INTO Dept (DeptNo, Dname, Loc) values (60, 'ENGINEERING', 'KANSAS CITY')";
command.ExecuteNonQuery();
transaction.Commit();
Console.WriteLine("Both records are written to database.");
}
catch (Exception e)
{
transaction.Rollback();
Console.WriteLine(e.ToString());
Console.WriteLine("Neither record was written to database.");
}
}
}
Public Sub RunOracleTransaction(ByVal connectionString As String)
Using connection As New OracleConnection(connectionString)
connection.Open()
Dim command As OracleCommand = connection.CreateCommand()
Dim transaction As OracleTransaction
' Start a local transaction
transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)
' Assign transaction object for a pending local transaction
command.Transaction = transaction
Try
command.CommandText = _
"INSERT INTO Dept (DeptNo, Dname, Loc) values (50, 'TECHNOLOGY', 'DENVER')"
command.ExecuteNonQuery()
command.CommandText = _
"INSERT INTO Dept (DeptNo, Dname, Loc) values (60, 'ENGINEERING', 'KANSAS CITY')"
command.ExecuteNonQuery()
transaction.Commit()
Console.WriteLine("Both records are written to database.")
Catch e As Exception
transaction.Rollback()
Console.WriteLine(e.ToString())
Console.WriteLine("Neither record was written to database.")
End Try
End Using
End Sub
備註
若要認可或回復交易,您必須明確使用 Commit 或 Rollback 方法。
若要確保適用於 Oracle 事務管理模型的數據提供者 .NET Framework 正確執行,請避免使用其他交易管理模型,例如資料庫所提供的模型。