ASP.net InnerException:将记录添加到数据库

发布于 2024-11-29 16:34:05 字数 925 浏览 6 评论 0原文

已阅读我的教科书,但当我尝试向数据库添加记录时,我仍然遇到捕获内部异常的问题。在我的类中捕获 InnerException 的正确语法是什么?

内心的期望被抛在 db.SaveChanges 上。以下是我的“addReviews.cs”类文件中用于添加记录的代码:

  public void addReview(int restId, int timeID, string dateValue, string describe, int cuisineId, int serviceId, int ambianceId, string memberId)
    {
        REVIEW addReview = new REVIEW();
        addReview.REVIEW_DATE = Convert.ToDateTime(dateValue);
        addReview.REVIEW_DATE_SUBMITTED = DateTime.Today;
        addReview.REVIEW_DESC = describe;
        addReview.TIME_ID = timeID;
        addReview.REVIEWER_ID = 1;
        addReview.FOOD_ID = cuisineId;
        addReview.SERVCE_ID = serviceId;
        addReview.AMBIENCE_ID = ambianceId;
        addReview.REST_ID = restId;
        addReview.PRICE_ID = 1;

        //Add record to database
        db.AddToREVIEWs(addReview);
        db.SaveChanges();
    }

提前致谢!

Been through my textbook, but I'm still having problems with catching an inner exception as I try to add a record to the database. What is the proper syntax for catching an InnerException in my class?

The inner expection is being thrown on the db.SaveChanges. Here is the code in my 'addReviews.cs' class file to add the record:

  public void addReview(int restId, int timeID, string dateValue, string describe, int cuisineId, int serviceId, int ambianceId, string memberId)
    {
        REVIEW addReview = new REVIEW();
        addReview.REVIEW_DATE = Convert.ToDateTime(dateValue);
        addReview.REVIEW_DATE_SUBMITTED = DateTime.Today;
        addReview.REVIEW_DESC = describe;
        addReview.TIME_ID = timeID;
        addReview.REVIEWER_ID = 1;
        addReview.FOOD_ID = cuisineId;
        addReview.SERVCE_ID = serviceId;
        addReview.AMBIENCE_ID = ambianceId;
        addReview.REST_ID = restId;
        addReview.PRICE_ID = 1;

        //Add record to database
        db.AddToREVIEWs(addReview);
        db.SaveChanges();
    }

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒尘 2024-12-06 16:34:05

试试这个:

try
{
  db.SaveChanges();
}
catch(ApplicationException exception)
{
   // just log and throw out
   System.Diagnostics.Debug.WriteLine(
              String.Concat(
                   "EXCEPTION: ", 
                   exception.ToString));

   if (exception.InnerException != null)
   {
      System.Diagnostics.Debug.WriteLine(
               String.Concat(
                  "INNER EXCEPTION: ", 
                  exception.InnerException.ToString));
   }

   throw;
}

PS:尝试捕获更具体的异常,在您的情况下,也值得捕获 SqlException

Try this out:

try
{
  db.SaveChanges();
}
catch(ApplicationException exception)
{
   // just log and throw out
   System.Diagnostics.Debug.WriteLine(
              String.Concat(
                   "EXCEPTION: ", 
                   exception.ToString));

   if (exception.InnerException != null)
   {
      System.Diagnostics.Debug.WriteLine(
               String.Concat(
                  "INNER EXCEPTION: ", 
                  exception.InnerException.ToString));
   }

   throw;
}

PS: try to catch more specific exception, in your case it worth to catch SqlException as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文