我正在尝试创建一个登录页面,当我给executereader它工作时,当我给executenonquery它返回一个-1值而不是1。
这在 cmd.executenonquery() 上返回 -1
SqlCommand cmd = new SqlCommand("select Count(*) from userDb where username= '"+txtusername.Text+"' and password= '"+txtpassword.Text+"'", con);
下面的代码与 executereader()
SqlCommand cmd = new SqlCommand("select Count(*) from userDb where username= @p1 and password= @p2", con);
**Complete Code**
SqlCommand cmd = new SqlCommand("select Count(*) from userDb where username= @p1 and password= @p2", con);
cmd.Parameters.AddWithValue("@p1", txtusername.Text);
cmd.Parameters.AddWithValue("@p2", txtpassword.Text);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read()==true)
{
FormsAuthentication.RedirectFromLoginPage(txtusername.Text, CheckBox1.Checked);
}
else
{
lbldisplay.Text = "Username and Password Do not Match";
}
con.Close();
带有 executenonquery 的代码
SqlCommand cmd = new SqlCommand("select Count(*) from userDb where username= '"+txtusername.Text+"' and password= '"+txtpassword.Text+"'", con);
con.Open();
int i = executenonquery();
if (i == 1)
{
FormsAuthentication.RedirectFromLoginPage(txtusername.Text, CheckBox1.Checked);
}
else
{
lbldisplay.Text = "Username and Password Do not Match";
}
con.Close();