C# 根据数据集验证电子邮件和密码输入

C# 根据数据集验证电子邮件和密码输入,c#,asp.net,validation,datatable,dataset,C#,Asp.net,Validation,Datatable,Dataset,我试图根据数据库中数据集的结果验证表单中的用户输入。我不知道我是在实现这个逻辑,还是在尝试foreach循环,我只是根据逻辑编出来的。我试图将数据集之外创建的DataTable的索引分配给变量,然后检查它们是否匹配用户输入。这是一种体面的方式吗?可能吗?我怎样才能做到这一点 我从我认为正确实现的代码中得到了一个错误,您可以在代码的注释中找到 受保护的无效btnSubmit\u Clickobject发件人,事件参数e { //为数据集创建连接,以填写电子邮件和密码进行验证 string Conn

我试图根据数据库中数据集的结果验证表单中的用户输入。我不知道我是在实现这个逻辑,还是在尝试foreach循环,我只是根据逻辑编出来的。我试图将数据集之外创建的DataTable的索引分配给变量,然后检查它们是否匹配用户输入。这是一种体面的方式吗?可能吗?我怎样才能做到这一点

我从我认为正确实现的代码中得到了一个错误,您可以在代码的注释中找到

受保护的无效btnSubmit\u Clickobject发件人,事件参数e { //为数据集创建连接,以填写电子邮件和密码进行验证 string ConnectionString=ConfigurationManager.ConnectionString[ContourCoffeeRoastersConnectionString].ConnectionString; SqlConnection Conn=新的SqlConnectionConnectionString; 康涅狄格开放大学

    //creats a data set with database information and puts the dataset in a datatable
    SqlDataAdapter daCustomer = new SqlDataAdapter("Select (CustEmail, CustPW) From Customer", Conn);
    DataSet dsEmailsandPW = new DataSet("Emails");
    daCustomer.Fill(dsEmailsandPW, "Customers");//I get and error here in my stack trace
    DataTable tblCustomers;
    tblCustomers = dsEmailsandPW.Tables["Customers"];

    //sets the variable to user inputed data from the login form so it can be compared and validated to the dataset
    string custEmail = exampleInputEmail1.Text;
    string custPW = exampleInputPassword1.Text;

    //looks through each row on the data set to see if a matching email can be found
    foreach (DataRow drCurrent in tblCustomers.Rows)
    {
        string txtEmail = drCurrent[0].ToString();//sets a variable to the first index of the current row of the dataset
        if (txtEmail == custEmail)//if a match is found with the user input and a record in the database through the data set the password is then checked for validation
        {
            string txtPW = drCurrent[1].ToString();//assigns a vaiable to the second index of the row that should contain customer password
            if (txtPW == custPW)//if the password is a match 
            {
                lblLogin.Text = "You are logged in!";
                //TODO: query for cartID and set it to the cookie!!!!! 
            }
            else
            {
                lblLogin.Text = "Email/username combination is not correct";
            }
        }
        else
        {
            lblLogin.Text = "Email/username combination is not correct";
        }
    }

我建议发送一个查询

"SELECT CustEmail FROM Customer where CustPW =" + custPw;
并将其作为布尔值进行计算。 如果返回true,则表明您拥有有效的登录名。 如果返回false,则表明您的登录无效

你也可以这样做

"SELECT CustEmail FROM Customer where CustPW =" + sha1(custPw);
如果密码已加密,则可以加密其尝试的登录密码并进行检查 针对数据库的加密密码

编辑:我还建议将查询存储为存储过程,只提供变量数据