首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >尝试将文本框中的数据与数据库中的数据进行比较时的错误消息

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

Stack Overflow用户
提问于 2017-12-07 14:06:19
回答 2查看 41关注 0票数 1

我正在尝试创建一个登录页面,其中我从Server数据库获取密码和电子邮件。我想比较一下密码和电子邮件。

代码语言:javascript
运行
复制
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");
            }
        }
    }
}

我收到以下错误消息:

System.Data.SqlClient.SqlException:‘必须声明标量变量“@email”。

我做错了什么?

EN

回答 2

Stack Overflow用户

发布于 2017-12-07 14:31:53

你的代码错了。您使用查询和连接定义了一个SqlDataAdapter,但是在尝试使用它填充DataTable之前,您不会对它做任何其他操作。它不知道@email@password的值是什么,因为你从来不告诉它。

您的代码应该如下所示:

代码语言:javascript
运行
复制
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");
                }
            }
        }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2017-12-07 14:10:38

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

代码语言:javascript
运行
复制
ophalen.Parameters.Add(new SqlParameter("@email", emailinlog));
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47696841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档