OdbcTransaction 类
表示要在数据源执行的 SQL 事务。无法继承此类。
**命名空间:**System.Data.Odbc
**程序集:**System.Data(在 system.data.dll 中)
语法
声明
Public NotInheritable Class OdbcTransaction
Inherits DbTransaction
用法
Dim instance As OdbcTransaction
public sealed class OdbcTransaction : DbTransaction
public ref class OdbcTransaction sealed : public DbTransaction
public final class OdbcTransaction extends DbTransaction
public final class OdbcTransaction extends DbTransaction
备注
该应用程序通过针对 OdbcConnection 对象调用 BeginTransaction 来创建 OdbcTransaction 对象。对 OdbcTransaction 对象执行与该事务关联的所有后续操作(例如提交或中止该事务)。
示例
下面的示例创建一个 OdbcConnection 和一个 OdbcTransaction。此示例还演示如何使用 BeginTransaction、Commit 和 Rollback 等方法。
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
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.
}
继承层次结构
System.Object
System.MarshalByRefObject
System.Data.Common.DbTransaction
System.Data.Odbc.OdbcTransaction
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1
请参见
参考
OdbcTransaction 成员
System.Data.Odbc 命名空间
OdbcDataAdapter 类
OdbcConnection 类