日期:2014-05-18  浏览次数:20466 次

验证用户登录!
protected void Button1_Click(object sender, EventArgs e)
  {
  SqlConnection con = DB.createcon();
  con.Open();
  SqlCommand sc = new SqlCommand("select * from userlogin where username='"+this.TextBox1.Text+"'AND userpass='"+this.TextBox2.Text+"'",con);

  int count = Convert.ToInt32(sc.ExecuteScalar());
  if (count != 1)
  {
  Response.Write("<script>alert('用户名密码错误')</script>");
  }
  else
  {
  Response.Redirect("success.aspx");
  } 







int count = Convert.ToInt32(sc.ExecuteScalar());
这句报出异常不知道怎么回事,希望各位帮我解决下,小弟在此谢谢了!!!!

------解决方案--------------------
SqlCommand sc = new SqlCommand("select count(id) from userlogin where username='"+this.TextBox1.Text+"'AND userpass='"+this.TextBox2.Text+"'",con);
------解决方案--------------------
用COunt就可以!
------解决方案--------------------
抱歉写错了 

ExecuteScalar 返回的是首行首列