【发布时间】:2018-12-31 04:50:01
【问题描述】:
我使用存储过程向数据库添加新订单,但在 ASP.NET 中调用该过程时找不到处理异常/错误的方法。 这是控制器的代码
public ActionResult Create(FormCollection collection)
{
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
SqlCommand com = new SqlCommand("OrderAdd", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@Cusid", collection["CustomerID"]);
//More Parameters
con.Open();
com.ExecuteNonQuery();
con.Close();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
然后尝试从过程中捕获代码
BEGIN CATCH
SELECT ERROR_MESSAGE(),ERROR_NUMBER(),ERROR_SEVERITY()
ROLLBACK TRAN @AddTran
END CATCH
该过程及其错误处理同样有效。所以问题是如何在控制器中捕获异常/错误。
【问题讨论】:
-
stackoverflow.com/questions/518292/…请查看以上链接处理错误
-
一个单纯的
catch通常从不是一个好的选择,尤其是当你忽略所有有价值的错误信息时。
标签: c# asp.net-mvc stored-procedures