事务解决方法
1.编程方法
string sql1 = "";
string sql2 = "";
SqlConnection conn = new SqlConnection(""); //建立数据库连接
conn.Open(); //接开数据库连接
SqlTransaction tran = conn.BeginTransaction(); //开始进行交易处理
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.Transaction = tran;
try
{
comm.CommandText = sql1;
comm.ExecuteNonQuery();
comm.CommandText = sql2;
comm.ExecuteNonQuery();
tran.Commit(); //接受交易,完成操作
}catch
{
tran.Rollback(); //交易失败,恢复数据
}
finally
{
conn.Close(); //关闭数据库连接。
}
string sql1 = "";
string sql2 = "";
SqlConnection conn = new SqlConnection(""); //建立数据库连接
conn.Open(); //接开数据库连接
SqlTransaction tran = conn.BeginTransaction(); //开始进行交易处理
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.Transaction = tran;
try
{
comm.CommandText = sql1;
comm.ExecuteNonQuery();
comm.CommandText = sql2;
comm.ExecuteNonQuery();
tran.Commit(); //接受交易,完成操作
}catch
{
tran.Rollback(); //交易失败,恢复数据
}
finally
{
conn.Close(); //关闭数据库连接。
}
2.数据库方法
BEGIN TRANSACTION
select * from student
select * form code
IF (@@ERROR > 0)
ROLLBACK TRANSACTION
ELSE
COMMIT TRANSACTION
BEGIN TRANSACTION
select * from student
select * form code
IF (@@ERROR > 0)
ROLLBACK TRANSACTION
ELSE
COMMIT TRANSACTION
posted on 2004-08-25 08:19 HelloSnoopy 阅读(409) 评论(0) 编辑 收藏 举报