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不存在。我不知道很好地利用它来解决问题。
答案 0 :(得分:0)
MySqlCommand
构造函数的第一个参数是命令文本。如果将代码更改为以下内容,它应该可以工作:
con.Open();
using (var command = new MySqlCommand())
{
command.Connection = con;
command.CommandText = @"SELECT level FROM userTable WHERE user=@username AND password=@password";