简体   繁体   English

尝试将文本框中的数据与数据库中的数据进行比较时出现错误消息

[英]Error message when trying to compare data from Textbox to data from database

I'm trying to make a login page where I take the Password and Email from a SQL Server database. 我试图建立一个登录页面,从SQL Server数据库获取密码和电子邮件。 I want to compare the password and the Email. 我想比较密码和电子邮件。

private void buttoninloggen_Click(object sender, EventArgs e)
{
    using (SqlConnection connection = new SqlConnection(connectionstring))
    {
        connection.Open();

        string emailinlog = textBoxEmailLogin.Text;
        string passwordinlog = textBoxPasswordLogin.Text;
        string vergelijken = "select * from Account where Email = @email and Password = @password";

        SqlDataAdapter adapter = new SqlDataAdapter(vergelijken, connection);

        MessageBox.Show("tot hier is t goed");

        using (SqlCommand ophalen = new SqlCommand(vergelijken, connection))
        {
            ophalen.Parameters.AddWithValue("@email", emailinlog);
            ophalen.Parameters.AddWithValue("@password", passwordinlog);

            DataTable tafel = new DataTable();
            adapter.Fill(tafel);

            if (tafel.Rows.Count > 0)
            {
                MessageBox.Show("ingelogd");
            }
        }
    }
}

I get this error message: 我收到此错误消息:

System.Data.SqlClient.SqlException: 'Must declare the scalar variable "@email".' System.Data.SqlClient.SqlException:'必须声明标量变量“ @email”。

What am I doing wrong? 我究竟做错了什么?

Your code is wrong. 您的代码是错误的。 You define an SqlDataAdapter with the query and connection but then do nothing else with it before trying to use it to fill the DataTable . 您可以使用查询和连接定义SqlDataAdapter ,但是在尝试使用它填充DataTable之前,不对其进行任何其他操作。 It has no idea what the values for @email or @password are because you never tell it. 不知道@email@password的值是什么,因为您从不告诉它。

Your code should look like this: 您的代码应如下所示:

private void buttoninloggen_Click(object sender, EventArgs e)
{
    using (SqlConnection connection = new SqlConnection(connectionstring))
    {
        connection.Open();
        string emailinlog = textBoxEmailLogin.Text;
        string passwordinlog = textBoxPasswordLogin.Text;
        string vergelijken = "select * from Account where Email = @email and Password = @password";

        // Moved the 'SqlDataAdapter' further down
        // SqlDataAdapter adapter = new SqlDataAdapter(vergelijken, connection);

        MessageBox.Show("tot hier is t goed");
        using (SqlCommand ophalen = new SqlCommand(vergelijken, connection))
        {
            ophalen.Parameters.AddWithValue("@email", emailinlog);
            ophalen.Parameters.AddWithValue("@password", passwordinlog);
            DataTable tafel = new DataTable();

            // SqlDataAdapter is now here
            // As it has been passed the SqlCommand it understands the parameters
            // Wrapped in using statement for disposal
            using (SqlDataAdapter adapter = new SqlDataAdapter(ophalen))
            {
                adapter.Fill(tafel);
                if (tafel.Rows.Count > 0)
                {
                    MessageBox.Show("ingelogd");
                }
            }
        }
    }
}

考虑从我的程序中将语法更改为工作代码:

ophalen.Parameters.Add(new SqlParameter("@email", emailinlog));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM