1 StringBuilder strSql = new StringBuilder();
2 strSql.AppendLine("begin tran tran_AddUserInfo --开始事务 ");
3 strSql.AppendLine("declare @tran_error int; ");
4 strSql.AppendLine("set @tran_error=0; ");
5 strSql.AppendLine("begin try ");
6 strSql.AppendLine("update PC_USER_T set Email='22' where id=3397 ");
7 strSql.AppendLine("update PC_USER_T set Email='111212111' where id=3396 ");
8 strSql.AppendLine("end try ");
9 strSql.AppendLine("begin catch ");
10 strSql.AppendLine(" set @tran_error=@tran_error+1; --加分号或不加都能正常执行 ");
11 strSql.AppendLine("end catch ");
12 strSql.AppendLine("if(@tran_error>0) ");
13 strSql.AppendLine("begin ");
14 strSql.AppendLine(" rollback tran; --执行出错,回滚事务(不指定事务名称) ");
15 strSql.AppendLine(" print @tran_error; ");
16 strSql.AppendLine("end ");
17 strSql.AppendLine("else ");
18 strSql.AppendLine("begin ");
19 strSql.AppendLine(" commit tran; --没有异常,提交事务(不指定事务名称) ");
20 strSql.AppendLine(" print @tran_error; ");
21 strSql.AppendLine("end ");