【问题标题】:I having problems with a user-password coding example我在使用用户密码编码示例时遇到问题
【发布时间】:2020-05-06 00:16:07
【问题描述】:

Using this earlier question I need a bit of help

使用上面链接中的第二个答案,我必须为 MySQL 更新它

 private void btLogin_Click(object sender, EventArgs e)
        {
            string connectionString;
            connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

            connection = new MySqlConnection(connectionString);
            using (var con = new MySqlConnection(connectionString));
            {
                using (var command = new MySqlCommand(connection = con))
                {
                    con.Open();

                    command.CommandText = @"SELECT level FROM userTable WHERE user=@username, password=@password";
                    command.Parameters.AddWithValue("@username", lbUser.Text);
                    command.Parameters.AddWithValue("@password", tbPassword.Text);

                    var strLevel = command.ExecuteScalar();

                    if (strLevel == DBNull.Value || strLevel == null)
                    {
                        MessageBox.Show("Invalid username or password");
                        return;
                    }
                    else
                    {
                        MessageBox.Show("Successfully login");
                        Hide(); // hide this form and show another form
                    }

                }
            }
        }

一切看起来都不错,但是这个

 using (var con = new MySqlConnection(connectionString));
            {
                using (var command = new MySqlCommand(connection = con))
                {
                    con.Open();

它说 con 不存在。不知道用那个井看问题。

【问题讨论】:

  • 我找到了 con 没有退出的原因;有一个 ;第一次使用后。但是我现在有了这个:'using (var command = new MySqlCommand(connection = con))' Error CS1503 Argument 1: cannot convert from 'MySql.Data.MySqlClient.MySqlConnection' to 'string' DPinBox2 C:\Users\de507\源\repos\DPinBox2\DPinBox2\LoginForm.cs 76 活动
  • 试试using (var command = new MySqlCommand(con))。我认为您不再需要 connection 变量了。

标签: c# mysql passwords


【解决方案1】:

MySqlCommand 构造函数的第一个参数是命令文本。如果您将代码更改为以下内容,它应该可以工作:

con.Open();
using (var command = new MySqlCommand())
{
    command.Connection = con;
    command.CommandText = @"SELECT level FROM userTable WHERE user=@username AND password=@password";

【讨论】:

  • 谢谢!不得不多玩一点,但这不是由您建议的代码引起的。
猜你喜欢
  • 2023-01-23
  • 1970-01-01
  • 1970-01-01
  • 2013-02-13
  • 1970-01-01
  • 1970-01-01
  • 2016-08-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多