当我输入RegiD
来办理登机手续时,它会将我带到我的表单2,但即使该号码不在我的SQL数据库的RegiD
列中,也会发生这种情况。如何检查我在文本框中输入的数字是否在我的SQL数据库中?
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-UHGLPQ8\SQLEXPRESS;Initial Catalog=Test Vol;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM MastVolData WHERE [RegiD] =' " + textBox1.Text + "'" , con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read() == true)
{
using (Form2 frm = new Form2(textBox1.Text))
{
frm.ShowDialog();
}
}
else
{
MessageBox.Show("Wrong I.D! Please try again!");
}
textBox1.Clear();
}
}
}
答案 0 :(得分:0)
首先,您需要停止连接字符串以创建SQL语句并开始使用参数化查询。否则你会冒SQL Injection次攻击的风险
第二,您需要了解何时使用using
声明
第三次,您需要了解HasRows
的{{1}}属性。
您的代码应该看起来像这样:
SqlDataReader