黑马程序员技术交流社区

标题: 向数据库添加数据时报错?不知道哪里出了问题 [打印本页]

作者: wang346351    时间: 2013-4-17 09:38
标题: 向数据库添加数据时报错?不知道哪里出了问题
本帖最后由 wang346351 于 2013-4-18 16:05 编辑


  1. string sql = "select StudentID,Name,Sex,Age,School,ByTime from Student";
  2. if (!string.IsNullOrEmpty(this.txtName.Text))
  3. {
  4. sql += "where Name like '%" + this.txtName.Text + "%'";
  5. }
  6. SqlConnection conn = new SqlConnection("Data Source=WANG-PC;Initial Catalog=WANG;User ID=sa;Password=123");
  7. conn.Open();
  8. SqlDataAdapter da=new SqlDataAdapter();
  9. da.SelectCommand=new SqlCommand();
  10. da.SelectCommand.Connection=conn;
  11. da.SelectCommand.CommandText=sql;
  12. da.SelectCommand.CommandType=CommandType.Text;
  13. DataSet ds = new DataSet();
  14. da.Fill(ds);
  15. conn.Close();
  16. conn.Dispose();
复制代码


sql语句好像没错啊  怎么老是报这个错误,,,,
作者: 清朗的晨风    时间: 2013-4-17 13:20
本帖最后由 清朗的晨风 于 2013-4-17 13:54 编辑

你看这行代码:
sql += "where Name like '%" + this.txtName.Text + "%'";
在where前加一个空格。。。
作者: 陈志盟    时间: 2013-4-17 13:51
sql += "where Name like '%" + this.txtName.Text + "%'";where前面加上空格,如果不加空格,拼接起来的sql语句就会是 ,自然查询语句会出错




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2