我试过研究这个话题,但它没有意义。我有一些代码尝试执行 UPDATE 查询,然后循环执行另一组 INSERT 查询。第二个查询错误,因为我省略了参数(目前这是故意引发错误)。UPDATE 仍然被提交,即使它不应该......
这是代码....
using (MySqlConnection con = existingCon.Clone())
{
con.Open();
MySqlParameter paramTimeStamp;
MySqlParameter paramFileNo;
MySqlParameter paramDescription;
MySqlParameter paramComment;
MySqlParameter paramUserInfo;
MySqlParameter paramPaymentType;
MySqlParameter paramSeriesLink;
MySqlParameter paramOriginalSubmitDate;
using (MySqlTransaction transact = con.BeginTransaction())
{
try
{
using (
MySqlCommand cmd =
new MySqlCommand(
"UPDATE Payments "
+ "SET strStatus = 'processing-preupload' "
+ "WHERE strStatus = 'processing-prepost';",
con))
{
cmd.Transaction = transact;
cmd.ExecuteNonQuery();
}
}
catch (Exception e)
{
transact.Rollback();
throw;
}
foreach (Payment payment in payments)
{
try
{
using (
MySqlCommand cmd =
new MySqlCommand(
"INSERT INTO Activity "
+ "(dtmTimeStamp, strFileNumber, strDescription, strComment, "
+ "strUserInfo, strPaymentType, dtmSeriesLink, dtmOriginalSubmitDate) "
+ "VALUES (@paramTimeStamp, @paramFileNo, @paramDescription, @paramComment, "
+ "@paramUserInfo, @paramPaymentType, @paramSeriesLink, @paramOriginalSubmitDate);",
con))
{
paramTimeStamp = new MySqlParameter("@paramTimeStamp", DateTime.Now);
paramFileNo = new MySqlParameter("@paramFileNo", payment.strFileNumber);
paramDescription = new MySqlParameter("@paramDescription", description);
paramComment = new MySqlParameter("@paramComment", comment);
paramUserInfo = new MySqlParameter("@paramUserInfo", userInfo);
paramPaymentType = new MySqlParameter("@paramPaymentType", PAYMENT_TYPE);
paramSeriesLink = new MySqlParameter(
"@paramSeriesLink",
payment.strSeriesLink_corrected);
paramOriginalSubmitDate = new MySqlParameter(
"@paramOriginalSubmitDate",
payment.strSubmitDate_corrected);
cmd.Transaction = transact;
cmd.ExecuteNonQuery();
}
}
catch (Exception e)
{
transact.Rollback();
throw;
}
}
transact.Commit();
}
}
想法?