string connectString;
// ...
// ...
using (OleDbConnection connection = new OleDbConnection(connectString))
{
connection.Open();
// Start a local transaction.
OleDbTransaction sqlTran = connection.BeginTransaction();
// Enlist the command in the current transaction.
OleDbCommand command = connection.CreateCommand();
command.Transaction = sqlTran;
try
{
command.CommandText =
"INSERT INTO Table1(Name) VALUES('Wrong size')";
command.ExecuteNonQuery();
command.CommandText =
"INSERT INTO Table2(Name) VALUES('Wrong color')";
command.ExecuteNonQuery();
sqlTran.Commit();
Console.WriteLine("Both records were written to database.");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("Neither record was written to database.");
try{sqlTran.Rollback();}catch{}
}
}