OdbcConnection.BeginTransaction Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Starts a transaction at the data source.
Overloads
BeginTransaction() |
Starts a transaction at the data source. |
BeginTransaction(IsolationLevel) |
Starts a transaction at the data source with the specified IsolationLevel value. |
BeginTransaction()
- Source:
- OdbcConnection.cs
- Source:
- OdbcConnection.cs
- Source:
- OdbcConnection.cs
Starts a transaction at the data source.
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
Returns
An object representing the new transaction.
Exceptions
A transaction is currently active. Parallel transactions are not supported.
Examples
The following example creates an OdbcConnection and an OdbcTransaction. It also demonstrates how to use the BeginTransaction, Commit, and Rollback methods.
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
Remarks
To commit or roll back the transaction, you must explicitly use the Commit or Rollback methods.
To make sure that the .NET Framework Data Provider for ODBC transaction management model performs correctly, avoid using other transaction management models, such as those provided by the data source.
Note
If you do not specify an isolation level, the isolation level will be determined by the driver being used. To specify an isolation level with the BeginTransaction method, use the overload that takes the isolevel
parameter.
See also
Applies to
BeginTransaction(IsolationLevel)
- Source:
- OdbcConnection.cs
- Source:
- OdbcConnection.cs
- Source:
- OdbcConnection.cs
Starts a transaction at the data source with the specified IsolationLevel value.
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
The transaction isolation level for this connection. If you do not specify an isolation level, the default isolation level for the driver is used.
Returns
An object representing the new transaction.
Exceptions
A transaction is currently active. Parallel transactions are not supported.
Examples
The following example creates an OdbcConnection and an OdbcTransaction. It also demonstrates how to use the BeginTransaction, Commit, and Rollback methods.
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
Remarks
To commit or roll back the transaction, you must explicitly use the Commit or Rollback methods.
To make sure that the .NET Framework Data Provider for ODBC transaction management model performs correctly, avoid using other transaction management models, such as those provided by the data source.