sql server中使用事务
银行转账的例子,用户0001向用户0002转帐10000元
begin transaction --开始
begin try
update NUser set Money = Money - 10000 where UserID='0001'
raiserror('更新有错误',16,1)
update NUser set Money = Money + 10000 where UserID='0002'
commit --提交
end try
begin catch
if @@TRANCOUNT >0 and @@error >0
select ERROR_LINE(),ERROR_MESSAGE(),ERROR_SEVERITY(),ERROR_STATE()
rollback--回滚
end catch
C#编码实现事务
SqlTransaction sqltran = null;
try
{
//1 获取界面用户输入数据
string userid_out = textBoxOut.Text.Trim();//转出用户账号
string userid_in = textBoxIn.Text.Trim();//转入用户账号
decimal amt = Convert.ToDecimal(textBoxAmt.Text.Trim());