我的问题总是在抛出。请帮我找到问题。谢谢!
MySqlConnection cnn = new MySqlConnection(mysqladdress);
cnn.Open();
try
{
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = cnn;
cmd.CommandText = "SELECT * FROM info where StudentID ='" + textBox1.Text + "'and Name='" + textBox2.Text + "'";
MySqlDataReader reader = cmd.ExecuteReader();
int count = 0;
while (reader.Read())
{
count = count + 1;
}
if (count == 1)
{
MessageBox.Show("Welcome");
}
else if (count > 1)
{
MessageBox.Show("Access Denied");
}
else
{
MessageBox.Show("Wrong student ID and Password");
}
}
catch (Exception)
{
throw;
}
答案 0 :(得分:2)
这必须是一个错字或简单的东西,如果不知道确切的错误信息很难说清楚。
为避免此类错误,我建议您使用参数:
cmd.CommandText = "SELECT * FROM info where StudentID = @StudentID and Name= @Name";
cmd.Parameters.Add("@StudentID", SqlDbType.VarChar).Value = textBox1.Text;
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = textBox2.Text;