Comme je l'ai lu Microsoft Jet (Access DB Engine) prend en charge les transactions. Ainsi, vous pouvez créer une transaction comme celui-ci (par exemple à partir CodeProject):
SqlConnection db = new SqlConnection("connstringhere");
SqlTransaction transaction;
db.Open();
transaction = db.BeginTransaction();
try
{
new SqlCommand("INSERT INTO TransactionDemo " +
"(Text) VALUES ('Row1');", db, transaction)
.ExecuteNonQuery();
new SqlCommand("INSERT INTO TransactionDemo " +
"(Text) VALUES ('Row2');", db, transaction)
.ExecuteNonQuery();
new SqlCommand("INSERT INTO CrashMeNow VALUES " +
"('Die', 'Die', 'Die');", db, transaction)
.ExecuteNonQuery();
transaction.Commit();
}
catch (SqlException sqlError)
{
transaction.Rollback();
}
db.Close();
Un moyen plus facile est (par exemple à partir 15 Seconds):
bool IsConsistent = false;
using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope())
{
SqlConnection cn = newSqlConnection(CONNECTION_STRING);
string sql = "DELETE Categories";
SqlCommand cmd = newSqlCommand(sql, cn);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
//Based on this property the transaction will commit if
//successful. If it fails however, this property will
//not be set and the transaction will not commit.
ts.Consistent = IsConsistent;
}
Vous aurez besoin MSDTC en cours d'exécution sur votre machine si vous utilisez TransactionScope.
Malheureusement, TableAdapter n'expose pas une propriété de connexion, vous avez donc besoin d'une solution de contournement. Donc, vous avez besoin de solution de contournement:
1) Réflexion (par exemple sous forme CodeProject)
conn = new SqlConnection(Properties.Settings.Default.NorthwindConnectionString);
conn.Open();
trans = conn.BeginTransaction();
1.
public SqlDataAdapter GetAdapter(object tableAdapter)
{
Type tableAdapterType = tableAdapter.GetType();
SqlDataAdapter adapter = (SqlDataAdapter)tableAdapterType.GetProperty("Adapter", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(tableAdapter, null);
return adapter;
}
2.
adapter.InsertCommand.Connection = trans.Connection;
adapter.UpdateCommand.Connection = trans.Connection;
adapter.DeleteCommand.Connection = trans.Connection;
3.
adapter.InsertCommand.Transaction = trans;
adapter.UpdateCommand.Transaction = trans;
adapter.DeleteCommand.Transaction = trans;
4.
-
5.
trans.commit();
La réflexion peut être très lent!
2) TransactionScope (par exemple sous forme DevX.com)
CustomersDataSet.CustomersDataTable customers = new CustomersDataSet.CustomersDataTable();
CustomersDataSetTableAdapters.CustomersTableAdapter tblAdap = new
CustomersDataSetTableAdapters.CustomersTableAdapter();
using (TransactionScope txScope = new TransactionScope())
{
tblAdap.Fill(customers);
customers.Rows[0]["ContactName"] = "Maria Velasquez";
tblAdap.Update(customers);
txScope.Complete();
}
Vous aurez besoin MSDTC!
Vous utilisez une MDB en tant que banque de données, tout ce qui est impliqué est Jet - Access n'est pas pertinent à votre question. –