OdbcTransaction 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示要对数据源进行的 SQL 事务。 无法继承此类。
public ref class OdbcTransaction sealed : System::Data::Common::DbTransaction
public ref class OdbcTransaction sealed : MarshalByRefObject, IDisposable, System::Data::IDbTransaction
public sealed class OdbcTransaction : System.Data.Common.DbTransaction
public sealed class OdbcTransaction : MarshalByRefObject, IDisposable, System.Data.IDbTransaction
type OdbcTransaction = class
inherit DbTransaction
type OdbcTransaction = class
inherit MarshalByRefObject
interface IDbTransaction
interface IDisposable
Public NotInheritable Class OdbcTransaction
Inherits DbTransaction
Public NotInheritable Class OdbcTransaction
Inherits MarshalByRefObject
Implements IDbTransaction, IDisposable
- 继承
- 继承
- 实现
示例
以下示例创建一个 OdbcConnection 和一个 OdbcTransaction。 它还演示如何使用 BeginTransaction、Commit和 Rollback 方法。
public static void ExecuteTransaction(string connectionString)
{
using (OdbcConnection connection =
new OdbcConnection(connectionString))
{
OdbcCommand command = new OdbcCommand();
OdbcTransaction transaction = null;
// Set the Connection to the new OdbcConnection.
command.Connection = connection;
// Open the connection and execute the transaction.
try
{
connection.Open();
// Start a local transaction
transaction = connection.BeginTransaction();
// Assign transaction object for a pending local transaction.
command.Connection = connection;
command.Transaction = transaction;
// Execute the commands.
command.CommandText =
"Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
command.ExecuteNonQuery();
command.CommandText =
"Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
command.ExecuteNonQuery();
// Commit the transaction.
transaction.Commit();
Console.WriteLine("Both records are written to database.");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
try
{
// Attempt to roll back the transaction.
transaction.Rollback();
}
catch
{
// Do nothing here; transaction is not active.
}
}
// The connection is automatically closed when the
// code exits the using block.
}
}
Public Sub ExecuteTransaction(ByVal connectionString As String)
Using connection As New OdbcConnection(connectionString)
Dim command As New OdbcCommand()
Dim transaction As OdbcTransaction
' Set the Connection to the new OdbcConnection.
command.Connection = connection
' Open the connection and execute the transaction.
Try
connection.Open()
' Start a local transaction.
transaction = connection.BeginTransaction()
' Assign transaction object for a pending local transaction.
command.Connection = connection
command.Transaction = transaction
' Execute the commands.
command.CommandText = _
"Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"
command.ExecuteNonQuery()
command.CommandText = _
"Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"
command.ExecuteNonQuery()
' Commit the transaction.
transaction.Commit()
Console.WriteLine("Both records are written to database.")
Catch ex As Exception
Console.WriteLine(ex.Message)
' Try to rollback the transaction
Try
transaction.Rollback()
Catch
' Do nothing here; transaction is not active.
End Try
End Try
' The connection is automatically closed when the
' code exits the Using block.
End Using
End Sub
注解
应用程序通过在 OdbcConnection 对象上调用 BeginTransaction 来创建 OdbcTransaction 对象。 与事务关联的所有后续操作(例如,提交或中止事务)在 OdbcTransaction 对象上执行。
属性
Connection |
获取与事务关联的 OdbcConnection 对象;如果事务不再有效,则 |
DbConnection |
在派生类中重写时,获取与事务关联的 DbConnection 对象。 (继承自 DbTransaction) |
IsolationLevel |
指定此事务的 IsolationLevel。 |
SupportsSavepoints |
获取一个值,该值指示此 DbTransaction 实例是否支持数据库保存点。
如果 |
方法
显式接口实现
IDbTransaction.Connection |
获取与事务关联的 DbConnection 对象;如果事务不再有效,则获取 null 引用。 (继承自 DbTransaction) |
IDisposable.Dispose() |
此 API 支持产品基础结构,不能在代码中直接使用。 释放 OdbcTransaction 类的当前实例使用的资源。 |
扩展方法
ConfigureAwait(IAsyncDisposable, Boolean) |
配置如何执行从异步可释放项返回的任务的 await。 |