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
- 继承
- 继承
- 实现
示例
以下示例创建一个 SqlConnection 和一个 SqlTransaction。 它还演示了如何使用BeginTransaction和CommitRollback方法。 事务在发生任何错误时回滚,或者在未首次提交的情况下释放该事务。
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处理时应始终使用异常处理。 如果 Commit 连接已终止,或者事务已在服务器上回滚,则同时 Rollback 生成一个 InvalidOperationException 连接。
有关SQL Server事务的详细信息,请参阅 Explicit Transactions 和 Coding Efficient Transactions。
属性
| 名称 | 说明 |
|---|---|
| Connection |
SqlConnection获取与事务关联的对象,或者 |
| DbConnection |
在派生类中重写时,获取 DbConnection 与事务关联的对象。 (继承自 DbTransaction) |
| IsolationLevel |
指定 IsolationLevel 此事务。 |
方法
| 名称 | 说明 |
|---|---|
| Commit() |
提交数据库事务。 |
| CreateObjRef(Type) |
创建一个对象,其中包含生成用于与远程对象通信的代理所需的所有相关信息。 (继承自 MarshalByRefObject) |
| Dispose() |
释放对象持有的资源。 |
| Dispose() |
释放由 < |
| 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) |
显式接口实现
| 名称 | 说明 |
|---|---|
| IDbTransaction.Connection |
获取与 DbConnection 事务关联的对象;如果事务不再有效,则获取 null 引用。 (继承自 DbTransaction) |