当前位置: 代码迷 >> ASP.NET >> 急数据库SELECT简单有关问题
  详细解决方案

急数据库SELECT简单有关问题

热度:5378   发布时间:2013-02-25 00:00:00.0
急,数据库SELECT简单问题
表名 users 里面有字段:id,username,password
  SqlConnection conn = new SqlConnection();
  conn.ConnectionString = 连接字符串;
  conn.Open();
  SqlCommand comm = conn.CreateCommand();
  comm.Connection = conn;
  comm.CommandText = "Select id,password from users wheres username='"+ TextBox1.Text.Trim()+"'";

 IDataReader read = comm.ExecuteReader();
  try
  {
  if (read.Read())
  {
  if (read.GetString(1) == name)
  {
  userid = read.GetInt32(0).ToString();
  Session["userid"] = userid;
  Response.Redirect("bbs.aspx");
  }
  else
  {
  Response.Write("<script>alter(‘用户密码不正确!’);</script>");
  }
  }
  else
  {
  Response.Write("<script>alter(‘没有此用户!’);</script>");
  }
  }
  finally
  {
  read.Close();
  conn.Close();
  conn.Dispose();
  }
  }
  }


运行时报错说:

'username' 附近有语法错误。

我找了一天还没解决,哪位朋友能指点下啊?


------解决方案--------------------------------------------------------
Select id,password from users wheres username='"+ TextBox1.Text.Trim()+"'"; 

where
------解决方案--------------------------------------------------------
C# code
   SqlConnection conn = new SqlConnection();         conn.ConnectionString = 连接字符串;         conn.Open();         SqlCommand comm = conn.CreateCommand();         comm.Connection = conn;         comm.CommandText = "Select id,password from users wheres username='"+ TextBox1.Text.Trim()+"'"; //"wheres"错误就在这儿。多了个SIDataReader read = comm.ExecuteReader();             try             {                 if (read.Read())                 {                     if (read.GetString(1) == name)                     {                         userid = read.GetInt32(0).ToString();                         Session["userid"] = userid;                         Response.Redirect("bbs.aspx");                     }                     else                     {                         Response.Write(" <script>alter(‘用户密码不正确!’); </script>");                     }                 }                 else                 {                     Response.Write(" <script>alter(‘没有此用户!’); </script>");                 }             }             finally             {                 read.Close();                 conn.Close();                 conn.Dispose();             }         }     }
------解决方案--------------------------------------------------------
Select id,password from users where username='"+ TextBox1.Text.Trim().Replace("'","")+"'
where 多了s
  相关解决方案