使用前面的问题,我需要一些帮助
使用上面链接中的第二个答案,我必须为 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();
它说骗局不存在。 我不知道用那个好看问题。
MySqlCommand
构造函数的第一个参数是命令文本。如果您将代码更改为以下内容,它应该可以工作:
con.Open();
using (var command = new MySqlCommand())
{
command.Connection = con;
command.CommandText = @"SELECT level FROM userTable WHERE user=@username AND password=@password";