在 第5行 string sql = "select * from a 下断点 看看最后的sql是什么另外 不要使用拼接字符串的方法 用SqlParameter http://www.cnblogs.com/angelfeather/articles/1225902.html
用这种方式试试: string sql = "select * from admin where(name=@users and pwd=@passwords)"; SqlParameter meter; SqlCommand cm = new SqlCommand(); meter = new SqlParameter("@users", user); cm.Parameters.Add(meter); meter = new SqlParameter("@passwords", pwd); cm.Parameters.Add(meter); int countResult = Convert.ToInt32(cm.ExecuteScalar());
ASP.NET SQL数据执行登录示例 //执行登录事件按钮,对照文本框输入信息,记录Session用户名密码验证码等! protected void btnLoad_Click(object sender, ImageClickEventArgs e) { HttpCookie cookie = Request.Cookies["CheckCode"]; if (String.Compare(cookie.Value, txtVali.Text, true) != 0) { Response.Write("<script lanuage=javascript>alert('验证码错误');location='javascript:history.go(-1)'</script>"); }//codego.net/tags/11/1/ else { DataSet ds = DB.reDs("select * from tb_HuenLian where UserName='" + txtUid.Text.Trim() + "' and PassWord='" + txtPwd.Text.Trim() + "'"); int i = this.checkLogin(txtUid.Text, txtPwd.Text); if (i > 0) { Session["id"] = ds.Tables[0].Rows[0][0].ToString(); Session["UserName"] = this.txtUid.Text; Session["PassWord"] = this.txtPwd.Text; Page.Response.Redirect("Yonghu.aspx"); } else { Response.Write("<script lanuage=javascript>alert('用户名称或密码错误!');location='javascript:history.go(-1)'</script>"); } } } //查询数据库数据信息验证登录 public int checkLogin(string loginName, string loginPwd) { SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["conn"]); SqlCommand myCommand = new SqlCommand("select count(*) from tb_HuenLian where UserName=@loginName and PassWord=@loginPwd", con); myCommand.Parameters.Add(new SqlParameter("@loginName", SqlDbType.NVarChar, 20)); myCommand.Parameters["@loginName"].Value = loginName; myCommand.Parameters.Add(new SqlParameter("@loginPwd", SqlDbType.NVarChar, 50)); myCommand.Parameters["@loginPwd"].Value = loginPwd; myCommand.Connection.Open(); int i = (int)myCommand.ExecuteScalar(); myCommand.Connection.Close(); return i; }
那请问有没办法判断查询到就跳.而不是统计有多少个
如if(true){...}
else{...}
这样.
那请问有没办法判断查询到就跳.而不是统计有多少个
如if(true){...}
else{...}
这样.
比如用SqlDataReader。结贴吧,不如去花时间看MSDN。
用SqlParameter
http://www.cnblogs.com/angelfeather/articles/1225902.html
string sql = "select * from admin where(name=@users and pwd=@passwords)";
SqlParameter meter;
SqlCommand cm = new SqlCommand();
meter = new SqlParameter("@users", user);
cm.Parameters.Add(meter);
meter = new SqlParameter("@passwords", pwd);
cm.Parameters.Add(meter);
int countResult = Convert.ToInt32(cm.ExecuteScalar());
//执行登录事件按钮,对照文本框输入信息,记录Session用户名密码验证码等!
protected void btnLoad_Click(object sender, ImageClickEventArgs e)
{
HttpCookie cookie = Request.Cookies["CheckCode"];
if (String.Compare(cookie.Value, txtVali.Text, true) != 0)
{ Response.Write("<script lanuage=javascript>alert('验证码错误');location='javascript:history.go(-1)'</script>");
}//codego.net/tags/11/1/ else
{
DataSet ds = DB.reDs("select * from tb_HuenLian where UserName='" + txtUid.Text.Trim() + "' and PassWord='" + txtPwd.Text.Trim() + "'");
int i = this.checkLogin(txtUid.Text, txtPwd.Text);
if (i > 0)
{
Session["id"] = ds.Tables[0].Rows[0][0].ToString();
Session["UserName"] = this.txtUid.Text;
Session["PassWord"] = this.txtPwd.Text;
Page.Response.Redirect("Yonghu.aspx");
}
else
{
Response.Write("<script lanuage=javascript>alert('用户名称或密码错误!');location='javascript:history.go(-1)'</script>");
}
}
}
//查询数据库数据信息验证登录
public int checkLogin(string loginName, string loginPwd)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["conn"]);
SqlCommand myCommand = new SqlCommand("select count(*) from tb_HuenLian where UserName=@loginName and PassWord=@loginPwd", con);
myCommand.Parameters.Add(new SqlParameter("@loginName", SqlDbType.NVarChar, 20));
myCommand.Parameters["@loginName"].Value = loginName;
myCommand.Parameters.Add(new SqlParameter("@loginPwd", SqlDbType.NVarChar, 50));
myCommand.Parameters["@loginPwd"].Value = loginPwd;
myCommand.Connection.Open();
int i = (int)myCommand.ExecuteScalar();
myCommand.Connection.Close();
return i;
}
断点跟,看这个到底返回的是什么东西