0

ここでは、leave イベント関数を使用してユーザーが入力したデータを検証したいので、ここでやりたいことは、ユーザーが入力したデータとデータベースを照合することです。ここでこのコードを使用して実行しましたが、正しく実行しているかどうかはわかりません。実行してみましたが、うまくいきません。

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);
        }

    }
}
4

1 に答える 1