我无法验证用户的登录,因为如果我输入错误的值,我的数据读取器不会被执行吗?
//代码
private void glassButton1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox1.Text == "" || comboBox1.SelectedIndex == 0)
{
Message m = new Message();
m.ShowDialog();
}
else
{
try
{
con.ConnectionString = "Data source=BLACK-PEARL;Initial Catalog=LIFELINE ;User id =sa; password=143";
con.Open();
SqlCommand cmd = new SqlCommand("Select LoginID,Password,Department from Login where LoginID=@loginID and Password=@Password and Department=@Department", con);
cmd.Parameters.Add(new SqlParameter("@loginID", textBox1.Text));
cmd.Parameters.Add(new SqlParameter("@Password", textBox2.Text));
cmd.Parameters.Add(new SqlParameter("@Department", comboBox1.Text));
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
string Strname = dr[0].ToString();
string StrPass = dr[1].ToString();
string StrDept = dr[2].ToString();
if(dr[2].ToString().Equals(comboBox1.Text)&&dr[0].ToString().Equals(textBox1.Text)&&dr[1].ToString().Equals(textBox2.Text))
{
MessageBox.Show("Welcome");
}
else
{
MessageBox.Show("Please Enter correct details");
}
}
dr.Close();
}
catch (Exception ex)
{
MessageBox.Show("Exception" + ex);
}
finally
{
con.Close();
}
}
}
//code
private void glassButton1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox1.Text == "" || comboBox1.SelectedIndex == 0)
{
Message m = new Message();
m.ShowDialog();
}
else
{
try
{
con.ConnectionString = "Data source=BLACK-PEARL;Initial Catalog=LIFELINE ;User id =sa; password=143";
con.Open();
SqlCommand cmd = new SqlCommand("Select LoginID,Password,Department from Login where LoginID=@loginID and Password=@Password and Department=@Department", con);
cmd.Parameters.Add(new SqlParameter("@loginID", textBox1.Text));
cmd.Parameters.Add(new SqlParameter("@Password", textBox2.Text));
cmd.Parameters.Add(new SqlParameter("@Department", comboBox1.Text));
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
string Strname = dr[0].ToString();
string StrPass = dr[1].ToString();
string StrDept = dr[2].ToString();
if(dr[2].ToString().Equals(comboBox1.Text)&&dr[0].ToString().Equals(textBox1.Text)&&dr[1].ToString().Equals(textBox2.Text))
{
MessageBox.Show("Welcome");
}
else
{
MessageBox.Show("Please Enter correct details");
}
}
dr.Close();
}
catch (Exception ex)
{
MessageBox.Show("Exception" + ex);
}
finally
{
con.Close();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当没有匹配的用户时,它将返回零行。
您应该检查阅读器是否有任何行,如果没有,则意味着登录详细信息错误。只需在该 if 语句中包含另一个 else 即可。
When there is no matching user it will return zero rows.
You should check if it reader has any rows if not then it means that login details are wrong. Just include another else with this if statement.
您应该在 SQL 中将
&&
替换为AND
:You should replace
&&
withAND
in your SQL: