我有3 delete
由我的sql命令执行的查询。如果在其他表中没有引用它,则只能执行三个查询中的最后一个。如果有参考,它将无法执行。所以,如果最后一次删除失败,我想rollback
不会丢失前两个命令中将要删除的数据。这就是为什么我需要使用SqlTransaction
。SQL事务总是被捕获
以下是我能够想到的。它实际上可以防止丢失任何数据,但是,我无法删除任何内容,并且总是得到弹出窗口(意味着每次执行它时都会被捕获)。
protected void editDelete(object sender, DirectEventArgs e)
{
SqlConnection MyConnection = new SqlConnection();
MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
MyConnection.Open();
// Start a local transaction.
SqlTransaction sqlTran = MyConnection.BeginTransaction();
try
{
SqlCommand MyCommand = new SqlCommand();
MyCommand.CommandText = /* 3 queries removed to shorten code */;
MyCommand.CommandType = CommandType.Text;
MyCommand.Connection = MyConnection;
SqlParameter p1 = new SqlParameter("@id", this.accountidEdit.Value);
MyCommand.Parameters.Add(p1);
MyCommand.ExecuteNonQuery();
MyConnection.Close();
/* reload and notification calls removed to shorten code */
}
catch (Exception)
{
sqlTran.Rollback();
X.Msg.Alert("Error", "Error.").Show();
}
}
此外,我试图通过查找问题:
catch (Exception sqlexception)
{
Console.WriteLine(sqlexception.Message);
sqlTran.Rollback();
X.Msg.Alert("Error", "Error.").Show();
}
但没有写过控制台..怪异?
这是执行SqlTransaction
的正确方法吗?我跟着a guide from the microsoft site来做到这一点。为什么它总是被抓到?
谢谢!
您需要在快乐'try'块中关闭连接之前提交SqlTransaction。异常(或InnerException)应该提及这一点。 – StuartLC 2014-10-16 15:20:31
此外,我认为你需要将'SqlCommand.Transaction'设置为'sqlTran'。 – juharr 2014-10-16 15:22:14
@StuartLC伟大的观察!我没有提交。然而,在'MyCommand.ExecuteNonQuery();'之后直接放置'sqlTran.Commit();'并不能解决问题 – starvator 2014-10-16 15:22:22