这里我想通过使用离开事件函数来验证用户输入的数据,所以我在这里要做的是匹配用户在数据库中输入的数据。我在这里使用这段代码来做到这一点,但我不确定我是否做得对。我试图运行它,但它不起作用。
private void cboBranch_Leave(object sender, EventArgs e)
{
if (cboBranch.Text.Length > 0)
{
try
{
using (MySqlConnection con = new MySqlConnection(serverstring))
{
string query = "SELECT * FROM tblBranches WHERE branch_name=@branch";
con.Open();
using (MySqlCommand cmd = new MySqlCommand(query, con))
{
using (MySqlDataReader dr = cmd.ExecuteReader())
{
cmd.Parameters.Add("@branch", MySqlDbType.VarChar, 30).Value = cboBranch.Text;
int count = 0;
bool flag = false;
while (dr.Read())
{
count++;
if (count == 1)
{
flag = true;
}
}
if (flag == false)
{
MessageBox.Show("The Branch name you entered does not match.", "AFICIONADO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
cboBranch.Select();
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}