如何使用Telerik ORM处理数据库异常

时间:2014-05-05 02:17:25

标签: c# orm telerik

我正在使用带有Telerik OpenAccess ORM的Visual Studio。现在我有代码检查数据库(通过ORM)以查看登录时是否存在用户。我目前正在测试不存在的用户,但这样做会引发异常。我想将请求包装在try-catch块中,以防止我的程序崩溃。

 public ActionResult Login(string password)
    {
        var db = ContextFactory.GetContextPerRequest();
        var username = Request.Form["username"].ToString();

        // try-catch (This line crashes the program)
        var user = (from u in db.Users where u.Username == username select u).Single();
        // end try-catch

        ViewBag.Login = new bool?(false);
        if (user.UserId != null)
        {
            ViewBag.Login = new bool?(true);
        }

        return View("Index");
    }

我很感激所给予的任何帮助。

0 个答案:

没有答案
相关问题