c#插入两条sql语句,怎么用事务处理的方法
展开全部
Sqlconnection conn=new Sqlconnection(str);
Sqlcommand cmd=conn.CreateCommand();
SqlTransaction tran;
conn.open();
tran=Conn.BeginTransaction(); //开始事务
cmd.Transaction=ston;
try
{
cmd.CommandText = sql语句;
cmd.ExecuteNonQuery();
cmd.CommandText = sql语句;
cmd.ExecuteNonQuery();
......
tran.commit(); //提交事务
}
catch()
{
tran.RollBack(); //事务回滚
}
finally
{
conn.Close();
}
Sqlcommand cmd=conn.CreateCommand();
SqlTransaction tran;
conn.open();
tran=Conn.BeginTransaction(); //开始事务
cmd.Transaction=ston;
try
{
cmd.CommandText = sql语句;
cmd.ExecuteNonQuery();
cmd.CommandText = sql语句;
cmd.ExecuteNonQuery();
......
tran.commit(); //提交事务
}
catch()
{
tran.RollBack(); //事务回滚
}
finally
{
conn.Close();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询