OdbcConnection.BeginTransaction Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Inicia uma transação na fonte de dados.
Sobrecargas
| Name | Description |
|---|---|
| BeginTransaction() |
Inicia uma transação na fonte de dados. |
| BeginTransaction(IsolationLevel) |
Inicia uma transação na fonte de dados com o valor especificado IsolationLevel . |
BeginTransaction()
Inicia uma transação na fonte de dados.
public:
System::Data::Odbc::OdbcTransaction ^ BeginTransaction();
public System.Data.Odbc.OdbcTransaction BeginTransaction();
member this.BeginTransaction : unit -> System.Data.Odbc.OdbcTransaction
override this.BeginTransaction : unit -> System.Data.Odbc.OdbcTransaction
Public Function BeginTransaction () As OdbcTransaction
Devoluções
Um objeto que representa a nova transação.
Exceções
Uma transação está atualmente ativa. Transações paralelas não são suportadas.
Exemplos
O exemplo seguinte cria um OdbcConnection e um OdbcTransaction. Também demonstra como usar os BeginTransactionmétodos , Commit, e Rollback .
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
Observações
Para confirmar ou reverter a transação, deve usar explicitamente os Commit métodos ou.Rollback
Para garantir que o modelo de gestão de transações .NET Framework Data Provider para ODBC funciona corretamente, evite usar outros modelos de gestão de transações, como os fornecidos pela fonte de dados.
Note
Se não especificar um nível de isolamento, o nível de isolamento será determinado pelo condutor utilizado. Para especificar um nível de isolamento com o BeginTransaction método, use a sobrecarga que assume o isolevel parâmetro.
Ver também
Aplica-se a
BeginTransaction(IsolationLevel)
Inicia uma transação na fonte de dados com o valor especificado IsolationLevel .
public:
System::Data::Odbc::OdbcTransaction ^ BeginTransaction(System::Data::IsolationLevel isolevel);
public System.Data.Odbc.OdbcTransaction BeginTransaction(System.Data.IsolationLevel isolevel);
member this.BeginTransaction : System.Data.IsolationLevel -> System.Data.Odbc.OdbcTransaction
override this.BeginTransaction : System.Data.IsolationLevel -> System.Data.Odbc.OdbcTransaction
Public Function BeginTransaction (isolevel As IsolationLevel) As OdbcTransaction
Parâmetros
- isolevel
- IsolationLevel
O nível de isolamento da transação para esta ligação. Se não especificar um nível de isolamento, é utilizado o nível de isolamento padrão para o driver.
Devoluções
Um objeto que representa a nova transação.
Exceções
Uma transação está atualmente ativa. Transações paralelas não são suportadas.
Exemplos
O exemplo seguinte cria um OdbcConnection e um OdbcTransaction. Também demonstra como usar os BeginTransactionmétodos , Commit, e Rollback .
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
Observações
Para confirmar ou reverter a transação, deve usar explicitamente os Commit métodos ou.Rollback
Para garantir que o modelo de gestão de transações .NET Framework Data Provider para ODBC funciona corretamente, evite usar outros modelos de gestão de transações, como os fornecidos pela fonte de dados.