ASP.net实体框架检查数据库中是否存在

时间:2016-02-18 08:21:29

标签: sql asp.net visual-studio-2015

我有VS2015,实体框架6.我有一个带有一个表的数据库(登录)

FirstName, lastName, Birthdate, email, password

我还有一个文本框,按钮

tbEmail tbpass and btnLogin

如何检查文本框中的用户电子邮件是否与数据库中的用户匹配?

到目前为止,我有:

 protected void btnLogin_Click(object sender, EventArgs e)
    {
            Logins Log = new Logins();

    using (LoginDataEntities lg = new LoginDataEntities())
    {
        string @email = tbUsernameL.Text;
        string @password = tbPassL.Text;

        var logged = from L in lg.Logins
                     where L.Username == @email
                     && L.Pass == @password
                     select L.Username;



        if (logged != null) 
        {
            lblSuccess.Visible = true;
        }
        else
        {
            lblFail.Visible = true;
        }


    }
}

然而,它不起作用并始终启用成功标签。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

使用以下代码段尝试一次:

using (LoginDataEntities lg = new LoginDataEntities())
        {
            string @email = tbUsernameL.Text;
            string @password = tbPassL.Text;

            var logged = lg.Logins
.SingleOrDefault(l=> l.Username == @email && l.Pass == @password);

            if (logged != null) // update
            {
                lblSuccess.Visible = true;
            }
            else
            {
                lblFail.Visible = true;
            }
        }

或者,您还可以再次查看以下示例:

http://www.c-sharpcorner.com/uploadfile/b19d5a/custom-user-login-and-registration-page-in-Asp-Net-mvc3-with-razor-and-entity-framework/

或者您使用个人用户帐户重构VS模板

enter image description here