通过实体框架登录

时间:2016-08-02 06:59:35

标签: entity-framework linq login

我尝试创建登录方法,在这个登录方法中,我尝试调用这样的存储过程

更新代码

当我尝试这个并点击按钮时,显示类似这样的东西..

  

' /'中的服务器错误应用

     

类型的异常' System.Exception'被扔了。

按钮代码:

protected void Button1_Click(object sender, EventArgs e)
{
        try
        {
            loginmethod(txt_us.Text, txt_pwd.Text);
        }
        catch( Exception )
        {
            throw new Exception();
        }
}

public bool  loginmethod(string UserName,string Password)
{
       TrackDataEntities1 td = new TrackDataEntities1();
       // splogin_Result sp=td.splogin(UserName,Password);
       splogin1_Result sp = td.splogin1(UserName, Password).FirstOrDefault();

       if(sp.Password == txt_pwd.Text)
       {
           return true;
       }
       else
       {
           return false;
       }
}

当我在模型中添加存储过程时,为splogin设置返回类型复杂类型,复杂名称为splogin1_Result,函数导入名称为splogin1

现在splogin1_Result

public partial class splogin1_Result : ComplexObject
{
    #region Factory Method

    /// <summary>
    /// Create a new splogin1_Result object.
    /// </summary>
    /// <param name="userName">Initial value of the UserName property.</param>
    /// <param name="password">Initial value of the Password property.</param>
    public static splogin1_Result Createsplogin1_Result(global::System.String userName, global::System.String password)
    {
        splogin1_Result splogin1_Result = new splogin1_Result();
        splogin1_Result.UserName = userName;
        splogin1_Result.Password = password;
        return splogin1_Result;
    }

    #endregion

    #region Simple Properties

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.String UserName
    {
        get
        {
            return _UserName;
        }
        set
        {
            OnUserNameChanging(value);
            ReportPropertyChanging("UserName");
            _UserName = StructuralObject.SetValidValue(value, false, "UserName");
            ReportPropertyChanged("UserName");
            OnUserNameChanged();
        }
    }
    private global::System.String _UserName;
    partial void OnUserNameChanging(global::System.String value);
    partial void OnUserNameChanged();

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.String Password
    {
        get
        {
            return _Password;
        }
        set
        {
            OnPasswordChanging(value);
            ReportPropertyChanging("Password");
            _Password = StructuralObject.SetValidValue(value, false, "Password");
            ReportPropertyChanged("Password");
            OnPasswordChanged();
        }
    }
    private global::System.String _Password;
    partial void OnPasswordChanging(global::System.String value);
    partial void OnPasswordChanged();

    #endregion
}

1 个答案:

答案 0 :(得分:0)

我试试这个,这对我有用

protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                loginmethod(txt_us.Text, txt_pwd.Text);
                Response.Redirect("WebForm1.aspx");
            }
            catch( Exception )
            {
                Label1.Text = ("Wrong info");
            }


        }
        public bool  loginmethod(string UserName,string Password)
        {
            T1 td = new T1();

         splogin1_Result sp = td.splogin1(UserName, Password).FirstOrDefault();
           if(sp.Password==txt_pwd.Text)
           {
               return true;
           }
           else
           {
               return false;
           }

        }
相关问题