OdbcConnection.BeginTransaction Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Startet eine Transaktion an der Datenquelle.
Überlädt
| Name | Beschreibung |
|---|---|
| BeginTransaction() |
Startet eine Transaktion an der Datenquelle. |
| BeginTransaction(IsolationLevel) |
Startet eine Transaktion an der Datenquelle mit dem angegebenen IsolationLevel Wert. |
BeginTransaction()
- Quelle:
- OdbcConnection.cs
- Quelle:
- OdbcConnection.cs
- Quelle:
- OdbcConnection.cs
- Quelle:
- OdbcConnection.cs
Startet eine Transaktion an der Datenquelle.
public:
System::Data::Odbc::OdbcTransaction ^ BeginTransaction();
public System.Data.Odbc.OdbcTransaction BeginTransaction();
override this.BeginTransaction : unit -> System.Data.Odbc.OdbcTransaction
member this.BeginTransaction : unit -> System.Data.Odbc.OdbcTransaction
Public Function BeginTransaction () As OdbcTransaction
Gibt zurück
Ein Objekt, das die neue Transaktion darstellt.
Ausnahmen
Eine Transaktion ist derzeit aktiv. Parallele Transaktionen werden nicht unterstützt.
Beispiele
Im folgenden Beispiel wird eine OdbcConnection und eine OdbcTransaction. Außerdem wird veranschaulicht, wie die BeginTransactionMethoden und CommitRollback Methoden verwendet werden.
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
Hinweise
Um die Transaktion zu übernehmen oder ein Rollback durchzuführen, müssen Sie die Commit Oder-Methoden Rollback explizit verwenden.
Um sicherzustellen, dass das .NET Framework-Datenanbieter für das ODBC-Transaktionsverwaltungsmodell ordnungsgemäß ausgeführt wird, vermeiden Sie die Verwendung anderer Transaktionsverwaltungsmodelle, z. B. derjenigen, die von der Datenquelle bereitgestellt werden.
Note
Wenn Sie keine Isolationsstufe angeben, wird die Isolationsstufe vom verwendeten Treiber bestimmt. Um eine Isolationsstufe mit der BeginTransaction Methode anzugeben, verwenden Sie die Überladung, die den isolevel Parameter verwendet.
Weitere Informationen
Gilt für:
BeginTransaction(IsolationLevel)
- Quelle:
- OdbcConnection.cs
- Quelle:
- OdbcConnection.cs
- Quelle:
- OdbcConnection.cs
- Quelle:
- OdbcConnection.cs
Startet eine Transaktion an der Datenquelle mit dem angegebenen IsolationLevel Wert.
public:
System::Data::Odbc::OdbcTransaction ^ BeginTransaction(System::Data::IsolationLevel isolevel);
public System.Data.Odbc.OdbcTransaction BeginTransaction(System.Data.IsolationLevel isolevel);
override this.BeginTransaction : System.Data.IsolationLevel -> System.Data.Odbc.OdbcTransaction
member this.BeginTransaction : System.Data.IsolationLevel -> System.Data.Odbc.OdbcTransaction
Public Function BeginTransaction (isolevel As IsolationLevel) As OdbcTransaction
Parameter
- isolevel
- IsolationLevel
Die Transaktionsisolationsstufe für diese Verbindung. Wenn Sie keine Isolationsstufe angeben, wird die Standardisolationsstufe für den Treiber verwendet.
Gibt zurück
Ein Objekt, das die neue Transaktion darstellt.
Ausnahmen
Eine Transaktion ist derzeit aktiv. Parallele Transaktionen werden nicht unterstützt.
Beispiele
Im folgenden Beispiel wird eine OdbcConnection und eine OdbcTransaction. Außerdem wird veranschaulicht, wie die BeginTransactionMethoden und CommitRollback Methoden verwendet werden.
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
Hinweise
Um die Transaktion zu übernehmen oder ein Rollback durchzuführen, müssen Sie die Commit Oder-Methoden Rollback explizit verwenden.
Um sicherzustellen, dass das .NET Framework-Datenanbieter für das ODBC-Transaktionsverwaltungsmodell ordnungsgemäß ausgeführt wird, vermeiden Sie die Verwendung anderer Transaktionsverwaltungsmodelle, z. B. derjenigen, die von der Datenquelle bereitgestellt werden.