sqltransaction,C#使用事务SqlTransaction操作sql语句
//开启一个事务
SqlConnection ConSelect = new SqlConnection("数据库连接字符串");
ConSelect.Open();
System.Data.SqlClient.SqlTransaction sTran = ConSelect.BeginTransaction();
try
{
//向数据表中添加数据
strSQL = "Insert Into UpdateYuECheck(CaseId,CheckDate,CheckUser) values("
+ SysPublic.SetMark(caseId) + ","
+ "getdate(),"
+ SysPublic.SetMark(strUser) + ")";
//执行事务操作
SqlHelper.ExecuteNonQuery(sTran, CommandType.Text, strSQL);
//修改数据表数据
strSQL = "Update [case] set NeedPayMoney = " + cb.encryptInfo.custRealBal
+ " where caseid = " + HG.Common.SysPublic.SetMark(caseId);
//执行事务操作
SqlHelper.ExecuteNonQuery(sTran, CommandType.Text, strSQL);
//修改数据表数据
strSQL = string.Format(@"update OUT0100002 set custRealBal='{0}',custRealRmbBal='{1}',custRealUsdBal='{2}'
,principal='{3}',custInstResidualPrincipal='{4}',custInstResidualBal='{5}'
,repayAmtSum='{6}' where custNbr='{7}'", cb.encryptInfo.custRealBal
, cb.encryptInfo.custRealRmbBal
, cb.encryptInfo.custRealUsdBal
, cb.encryptInfo.principal
, cb.encryptInfo.custInstResidualPrincipal
, cb.encryptInfo.custInstResidualBal
, cb.encryptInfo.repayAmtSum
, cb.encryptInfo.custNbr);
//执行事务操作
SqlHelper.ExecuteNonQuery(sTran, CommandType.Text, strSQL);
}
}
//提交事物
sTran.Commit();
iR = 0;
}
catch (System.Exception ex)
{
//回滚事务
sTran.Rollback();
strError = ex.Message.ToString();
}
原文链接:https://blog.csdn.net/qq_34576513/article/details/103008011