SqlTransaction 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表一個在 SQL Server 資料庫中進行的 Transact-SQL 交易。 此類別無法獲得繼承。
public ref class SqlTransaction sealed : MarshalByRefObject, IDisposable, System::Data::IDbTransaction
public ref class SqlTransaction sealed : System::Data::Common::DbTransaction
public sealed class SqlTransaction : MarshalByRefObject, IDisposable, System.Data.IDbTransaction
public sealed class SqlTransaction : System.Data.Common.DbTransaction
type SqlTransaction = class
inherit MarshalByRefObject
interface IDbTransaction
interface IDisposable
type SqlTransaction = class
inherit DbTransaction
Public NotInheritable Class SqlTransaction
Inherits MarshalByRefObject
Implements IDbTransaction, IDisposable
Public NotInheritable Class SqlTransaction
Inherits DbTransaction
- 繼承
- 繼承
- 實作
範例
以下範例會產生 a SqlConnection 和 SqlTransaction。 同時示範如何使用 BeginTransaction、 Commit及 Rollback 方法。 交易在任何錯誤時或未先提交就被處置時會被回滾。
Try
/
Catch 錯誤處理用於在嘗試提交或回滾交易時處理錯誤。
private static void ExecuteSqlTransaction(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = connection.CreateCommand();
SqlTransaction transaction;
// Start a local transaction.
transaction = connection.BeginTransaction();
// Must assign both transaction object and connection
// to Command object for a pending local transaction
command.Connection = connection;
command.Transaction = transaction;
try
{
command.CommandText =
"Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
command.ExecuteNonQuery();
command.CommandText =
"Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
command.ExecuteNonQuery();
// Attempt to commit the transaction.
transaction.Commit();
Console.WriteLine("Both records are written to database.");
}
catch (Exception ex)
{
Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
Console.WriteLine(" Message: {0}", ex.Message);
// Attempt to roll back the transaction.
try
{
transaction.Rollback();
}
catch (Exception ex2)
{
// This catch block will handle any errors that may have occurred
// on the server that would cause the rollback to fail, such as
// a closed connection.
Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
Console.WriteLine(" Message: {0}", ex2.Message);
}
}
}
}
Private Sub ExecuteSqlTransaction(ByVal connectionString As String)
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim command As SqlCommand = connection.CreateCommand()
Dim transaction As SqlTransaction
' Start a local transaction
transaction = connection.BeginTransaction()
' Must assign both transaction object and connection
' to Command object for a pending local transaction.
command.Connection = connection
command.Transaction = transaction
Try
command.CommandText = _
"Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"
command.ExecuteNonQuery()
command.CommandText = _
"Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"
command.ExecuteNonQuery()
' Attempt to commit the transaction.
transaction.Commit()
Console.WriteLine("Both records are written to database.")
Catch ex As Exception
Console.WriteLine("Commit Exception Type: {0}", ex.GetType())
Console.WriteLine(" Message: {0}", ex.Message)
' Attempt to roll back the transaction.
Try
transaction.Rollback()
Catch ex2 As Exception
' This catch block will handle any errors that may have occurred
' on the server that would cause the rollback to fail, such as
' a closed connection.
Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType())
Console.WriteLine(" Message: {0}", ex2.Message)
End Try
End Try
End Using
End Sub
備註
應用程式透過呼叫BeginTransactionSqlConnection物件來建立SqlTransaction物件。 所有與該交易相關的後續操作(例如,提交或中止交易)都會在物件上 SqlTransaction 執行。
Note
Try
/
Catch 在提交或回 SqlTransaction滾 . 如果連線終止或交易已經在伺服器上回滾,兩者都會CommitRollback產生 。InvalidOperationException
欲了解更多SQL Server交易資訊,請參閱 Explicit Transactions 及 Coding Efficient Transactions。
屬性
| 名稱 | Description |
|---|---|
| Connection |
取得 SqlConnection 與交易相關的物件,或 |
| DbConnection |
當在派生類別中覆寫時,會取得 DbConnection 與該交易相關的物件。 (繼承來源 DbTransaction) |
| IsolationLevel |
指定此交易的 。IsolationLevel |
方法
| 名稱 | Description |
|---|---|
| Commit() |
提交資料庫交易。 |
| CreateObjRef(Type) |
建立一個物件,包含產生代理伺服器所需的所有相關資訊,用於與遠端物件通訊。 (繼承來源 MarshalByRefObject) |
| Dispose() |
釋放物件所持有的資源。 |
| Dispose() |
釋放 DbTransaction. (繼承來源 DbTransaction) |
| Dispose(Boolean) |
釋放 未管理的資源, DbTransaction 並可選擇性地釋放受管理資源。 (繼承來源 DbTransaction) |
| Equals(Object) |
判斷指定的 物件是否等於目前的物件。 (繼承來源 Object) |
| GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
| GetLifetimeService() |
取得目前控制此實例生命週期政策的終身服務物件。 (繼承來源 MarshalByRefObject) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| InitializeLifetimeService() |
取得一個終身服務物件以控制此實例的終身政策。 (繼承來源 MarshalByRefObject) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| MemberwiseClone(Boolean) |
建立一個 MarshalByRefObject 目前物件的淺層複製品。 (繼承來源 MarshalByRefObject) |
| Rollback() |
會將交易從待處理狀態回滾。 |
| Rollback(String) |
從待處理狀態回滾交易,並指定交易名稱或儲存點名稱。 |
| Save(String) |
在交易中建立一個儲存點,可用來回滾交易的一部分,並指定儲存點名稱。 |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |
明確介面實作
| 名稱 | Description |
|---|---|
| IDbTransaction.Connection |
取得 DbConnection 與交易相關的物件,或在交易不再有效時取得空參考。 (繼承來源 DbTransaction) |