OdbcConnection.BeginTransaction Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Start een transactie bij de gegevensbron.
Overloads
| Name | Description |
|---|---|
| BeginTransaction() |
Start een transactie bij de gegevensbron. |
| BeginTransaction(IsolationLevel) |
Start een transactie bij de gegevensbron met de opgegeven IsolationLevel waarde. |
BeginTransaction()
- Bron:
- OdbcConnection.cs
- Bron:
- OdbcConnection.cs
- Bron:
- OdbcConnection.cs
- Bron:
- OdbcConnection.cs
Start een transactie bij de gegevensbron.
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
Retouren
Een object dat de nieuwe transactie vertegenwoordigt.
Uitzonderingen
Er is momenteel een transactie actief. Parallelle transacties worden niet ondersteund.
Voorbeelden
In het volgende voorbeeld wordt een OdbcConnection en een OdbcTransaction. Het laat ook zien hoe u de BeginTransaction, Commiten Rollback methoden gebruikt.
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
Opmerkingen
Als u de transactie wilt doorvoeren of terugdraaien, moet u de of Commit methoden expliciet gebruikenRollback.
Om ervoor te zorgen dat het .NET Framework Data Provider voor het ODBC-transactiebeheermodel correct wordt uitgevoerd, vermijdt u het gebruik van andere modellen voor transactiebeheer, zoals die worden geleverd door de gegevensbron.
Note
Als u geen isolatieniveau opgeeft, wordt het isolatieniveau bepaald door het stuurprogramma dat wordt gebruikt. Als u een isolatieniveau met de BeginTransaction methode wilt opgeven, gebruikt u de overbelasting die de isolevel parameter gebruikt.
Zie ook
Van toepassing op
BeginTransaction(IsolationLevel)
- Bron:
- OdbcConnection.cs
- Bron:
- OdbcConnection.cs
- Bron:
- OdbcConnection.cs
- Bron:
- OdbcConnection.cs
Start een transactie bij de gegevensbron met de opgegeven IsolationLevel waarde.
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
Parameters
- isolevel
- IsolationLevel
Het niveau van transactieisolatie voor deze verbinding. Als u geen isolatieniveau opgeeft, wordt het standaardisolatieniveau voor het stuurprogramma gebruikt.
Retouren
Een object dat de nieuwe transactie vertegenwoordigt.
Uitzonderingen
Er is momenteel een transactie actief. Parallelle transacties worden niet ondersteund.
Voorbeelden
In het volgende voorbeeld wordt een OdbcConnection en een OdbcTransaction. Het laat ook zien hoe u de BeginTransaction, Commiten Rollback methoden gebruikt.
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
Opmerkingen
Als u de transactie wilt doorvoeren of terugdraaien, moet u de of Commit methoden expliciet gebruikenRollback.
Om ervoor te zorgen dat het .NET Framework Data Provider voor het ODBC-transactiebeheermodel correct wordt uitgevoerd, vermijdt u het gebruik van andere modellen voor transactiebeheer, zoals die worden geleverd door de gegevensbron.