OleDbConnection.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.
Hiermee start u een databasetransactie.
Overloads
| Name | Description |
|---|---|
| BeginTransaction() |
Hiermee start u een databasetransactie met de huidige IsolationLevel waarde. |
| BeginTransaction(IsolationLevel) |
Hiermee start u een databasetransactie met het opgegeven isolatieniveau. |
BeginTransaction()
Hiermee start u een databasetransactie met de huidige IsolationLevel waarde.
public:
System::Data::OleDb::OleDbTransaction ^ BeginTransaction();
public System.Data.OleDb.OleDbTransaction BeginTransaction();
member this.BeginTransaction : unit -> System.Data.OleDb.OleDbTransaction
override this.BeginTransaction : unit -> System.Data.OleDb.OleDbTransaction
Public Function BeginTransaction () As OleDbTransaction
Retouren
Een object dat de nieuwe transactie vertegenwoordigt.
Uitzonderingen
Parallelle transacties worden niet ondersteund.
Voorbeelden
In het volgende voorbeeld wordt een OleDbConnection en een OleDbTransaction. Het laat ook zien hoe u de BeginTransaction, Commiten Rollback methoden gebruikt.
public void ExecuteTransaction(string connectionString)
{
using (OleDbConnection connection =
new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand();
OleDbTransaction transaction = null;
// Set the Connection to the new OleDbConnection.
command.Connection = connection;
// Open the connection and execute the transaction.
try
{
connection.Open();
// Start a local transaction with ReadCommitted isolation level.
transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
// 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 OleDbConnection(connectionString)
Dim command As New OleDbCommand()
Dim transaction As OleDbTransaction
' Set the Connection to the new OleDbConnection.
command.Connection = connection
' Open the connection and execute the transaction.
Try
connection.Open()
' Start a local transaction with ReadCommitted isolation level.
transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)
' 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
U moet de transactie expliciet doorvoeren of terugdraaien met behulp van de Commit of Rollback methode. Om ervoor te zorgen dat het .NET Framework Data Provider voor OLE DB-transactiebeheermodel correct wordt uitgevoerd, vermijdt u het gebruik van andere modellen voor transactiebeheer, zoals die van de gegevensbron.
Zie ook
Van toepassing op
BeginTransaction(IsolationLevel)
Hiermee start u een databasetransactie met het opgegeven isolatieniveau.
public:
System::Data::OleDb::OleDbTransaction ^ BeginTransaction(System::Data::IsolationLevel isolationLevel);
public System.Data.OleDb.OleDbTransaction BeginTransaction(System.Data.IsolationLevel isolationLevel);
member this.BeginTransaction : System.Data.IsolationLevel -> System.Data.OleDb.OleDbTransaction
override this.BeginTransaction : System.Data.IsolationLevel -> System.Data.OleDb.OleDbTransaction
Public Function BeginTransaction (isolationLevel As IsolationLevel) As OleDbTransaction
Parameters
- isolationLevel
- IsolationLevel
Het isolatieniveau waaronder de transactie moet worden uitgevoerd.
Retouren
Een object dat de nieuwe transactie vertegenwoordigt.
Uitzonderingen
Parallelle transacties worden niet ondersteund.
Voorbeelden
In het volgende voorbeeld wordt een OleDbConnection en een OleDbTransaction. Het laat ook zien hoe u de BeginTransaction, a Commiten Rollback methoden gebruikt.
public void ExecuteTransaction(string connectionString)
{
using (OleDbConnection connection =
new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand();
OleDbTransaction transaction = null;
// Set the Connection to the new OleDbConnection.
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 OleDbConnection(connectionString)
Dim command As New OleDbCommand()
Dim transaction As OleDbTransaction
' Set the Connection to the new OleDbConnection.
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
U moet de transactie expliciet doorvoeren of terugdraaien met behulp van de Commit of Rollback methode. Om ervoor te zorgen dat het .NET Framework Data Provider voor OLE DB-transactiebeheermodel correct wordt uitgevoerd, vermijdt u het gebruik van andere modellen voor transactiebeheer, zoals die van de gegevensbron.
Note
Als u geen isolatieniveau opgeeft, wordt het standaardisolatieniveau voor de onderliggende provider gebruikt. Als u een isolatieniveau met de BeginTransaction methode wilt opgeven, gebruikt u de overbelasting die de isolationLevel parameter gebruikt.