首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

小白:大家看一下一个关于ExecuteScalar()的异常

2012-02-19 
小白:大家看一下一个关于ExecuteScalar()的错误。出现一个错误,请高手指点。登陆的页面,.cs页面代码如下:Sql

小白:大家看一下一个关于ExecuteScalar()的错误。
出现一个错误,请高手指点。
登陆的页面,.cs页面代码如下:
 SqlConnection conn = new SqlConnection("server=.\\SQLEXPRESS;database=bookdata;uid=sa;pwd=xiaobai;");
  SqlCommand cmd = new SqlCommand("select count(id) from userinfo" + "where username='" + TextBoxUid.Text +
  "'and password='" + TextBoxPwd.Text + "'", conn);
  conn.Open();

  int iCount=Convert.ToInt32(cmd.ExecuteScalar());
  if (iCount > 0)
  {

  Response.Redirect("index.aspx");

  }
  else
  {
  LabelErrMsg.Text = "登陆错误";
  }
预览出现错误:'=' 附近有语法错误。
行 25: conn.Open();
行 26: 
行 27: int iCount=Convert.ToInt32(cmd.ExecuteScalar());行 28: if (iCount > 0)
行 29: {

[解决办法]
这句有问题应该要有空格才行:

SqlCommand cmd = new SqlCommand("select count(id) from userinfo " + " where username='" + TextBoxUid.Text + 
"' and password='" + TextBoxPwd.Text + "'", conn);
[解决办法]
这个错误是 sql语句错误。
保证你的TextBox都有值。 且注意你的空格。楼上已经加好。
SqlCommand cmd = new SqlCommand("select count(id) from userinfo " + " where username='" + TextBoxUid.Text + 
"' and password='" + TextBoxPwd.Text + "'", conn);
[解决办法]
//学会用参数而不是拼接SQL语句
SqlCommand cmd = new SqlCommand("select count(id) from userinfo where username= @username and [password] = @password", conn);
cmd.Parameters.AddWithValue("@username", TextBoxUid.Text);
cmd.Parameters.AddWithValue("@password", TextBoxPwd.Text);

热点排行