获取并设置 MySQL Select

Get & Set MySQL Select

我在这里遗漏了什么,似乎主要形式没有从 tsDBCon class 调用方法并且没有连接到数据库,我需要帮助,我是 get:set 属性的新手,我做错了吗?谢谢。

public class tsDBCon
    {
    private string _user;
    private string _pass;

           public string User
           {
               get 
               {
                   return _user;
               }
               set
               {
                   _user = value;
               }
           }

           public string Pass
           {
               get 
               { 
                   return _pass; 
               }
               set
               {
                   _pass = value;
               }
           }


    public void QueryUser()
        {
            tsDBCon get = new tsDBCon();  
            var connSettings = ConfigurationManager.ConnectionStrings["MyDB"];
            {
                string CN = connSettings.ConnectionString;
                MySqlConnection conn = new MySqlConnection(CN);
                MySqlCommand cmd = new MySqlCommand("select * from users where user = @user and pass = @pass", conn);
                cmd.Parameters.AddWithValue("@user", get.User);
                cmd.Parameters.AddWithValue("@pass", get.Pass);
                MySqlDataReader reader = cmd.ExecuteReader();
                conn.Open();
                while (reader.Read())
                {
                    get.User = reader["user"].ToString();
                    get.Pass = reader["pass"].ToString();
                }
            }
        }
    }


     This is the code in Main Form
     tsDBCon ts = new tsDBCon();
     private void GetUser()
     {
      ts.User = txtUser.Text;
      ts.Pass = txtPass.Text;
     }

What am I missing here, it seems the main form is not calling the method from tsDBCon class and not connecting to the database, I need help, i'm new to get:set properties, am i doing it wrong or what?

是的,你做错了几件事。

I want to validate if the user entered the correct user and pass through the textboxes.

您的代码存在几个问题:

  • 当您创建 IDisposable 个实例(例如 MySqlConnection)时,您需要处置它们。确保这一点的最简单方法是将它们包装在 using 块中。
  • 如果您创建一个对象的本地临时实例(例如 tsDBCon get = new tsDBCon();),您分配给它的属性的任何值都将在您离开您所在方法的范围后消失。除非您 return 它作为函数结果或通过 refout 参数。

要检查用户名/密码组合是否正确,根据您提供的代码示例,您可能需要像这样更改一些内容:

public class tsDBCon
{
    private const string ExistsQuery = "select count(*) from users where user = @user and pass = @pass";

    public tsDBCon(string user, string pass)
    {
        User = user;
        Pass = pass;
    }

    public string User { get; set; }
    public string Pass { get; set; }

    public bool LoginExists()
    {
        var connSettings = ConfigurationManager.ConnectionStrings["MyDB"];
        var CN = connSettings.ConnectionString;

        using (var conn = new MySqlConnection(CN))
        {
            conn.Open();
            using (cmd = new MySqlCommand(ExistsQuery, conn))
            {
                cmd.Parameters.AddWithValue("@user", User);
                cmd.Parameters.AddWithValue("@pass", Pass);
                var count = (int)cmd.ExecuteScalar();
                return count > 0;
            }
        }
    }
}

然后在您的主窗体中像这样使用它:

ts = new tsDBCon(txtUser.Text, txtPass.Text);
userLoginExists = ts.LoginExists();

最后一点:永远不要存储未加密的密码。