对于分布式事务的处理与本地事务的处理有很大的区别,首先必须保证你所使用的数据库必须支持对分布式事务的支持,有些数据库只支持本地事务
private void AddItem(int id, int money, Transaction trans)
{
SqlConnection conn = new SqlConnection(Str_DataBase);
conn.Open();
conn.EnlistTransaction(trans);
SqlCommand command = conn.CreateCommand();
command.CommandText = "INSERT INTO [t1] VALUES(@id,@money)";
command.Parameters.AddWithValue("@id", id);
command.Parameters.AddWithValue("@money", money);
command.ExecuteNonQuery();
conn.Close();
}
private void button3_Click(object sender, EventArgs e)
{
try
{
CommittableTransaction trans = new CommittableTransaction();
AddItem(3, 3, trans);
AddItem(4, 4, trans);
trans.Rollback();
}
catch (Exception e_con)
{
MessageBox.Show(e_con.Message);
}
}