conn.Open();
                costr = "select flightID as 航班号,Dcity as 出发城市,tcity as 到达城市 from flight where flightID like'" + this.textBox5.Text + "'" + "and  Dcity like '" + this.textBox1.Text + "'" + "and  Tcity like '" + this.textBox2.Text + "'";
                myc = new SqlCommand(costr, conn);
                myda = new DataSet();
                myd = new SqlDataAdapter(myc);
                myd.Fill(myda, "flight");
                this.dataGridView1.DataSource = this.myda.Tables["flight"];要实现任意textbox中输入内容都可查询信息,输入多少个就符合多少个
                myd.Fill(myda, "flight");
这句有异常

解决方案 »

  1.   

     costr = "select flightID as 航班号,Dcity as 出发城市,tcity as 到达城市 from flight where flightID like'%" + this.textBox5.Text + "%' and  Dcity like '%" +this.textBox1.Text + "%' and  Tcity like '%" + this.textBox2.Text + "%'"; 
      

  2.   

                    conn.Open();
                    costr = "select flightID as 航班号,uclassprice as 上等舱价格,Yclassprice as 经济舱价格,reuclassno as 上等舱剩余票数,reyclassno as 经济舱剩余票数 from flight where flightID='" + this.textBox5.Text + "'";
                    myc = new SqlCommand(costr, conn);
                    myda = new DataSet();
                    myd = new SqlDataAdapter(myc);
                    myd.Fill(myda, "flight");
                    this.dataGridView1.DataSource = this.myda.Tables["flight"];同样的这段 没有任何问题显示
    未处理的 SQL.exception
    第 1 行: ' ' 附近有语法错误。
      

  3.   

    costr = "select flightID as 航班号,Dcity as 出发城市,tcity as 到达城市 from flight where flightID like'%" + this.textBox5.Text + "%'" + "or  Dcity like '%" + this.textBox1.Text + "%'" + "or  Tcity like '%" + this.textBox2.Text + "%'";要实现任意textbox 输入都可查询 用and 应该查不出来数据的把
     如果textbox5用数据 而 textbox1为空 那么就查不出来的  
    我想你数据库的字段没有为null的把 用 or 可以实现
      

  4.   

    楼主这样使用字符连接SQL查询语句,如果this.textBox5.Text含有单引号'就会出错吧
      

  5.   

    晕死了,跟踪一下不就行了。查看一下costr的值不就可以了嘛!肉眼看不出问题,你可以放到SqlServer中去执行这条语句
      

  6.   

    放在SQLsever 中还是同样的
    第 3 行: ' ' 附近有语法错误。
      

  7.   

    这是我以前写过的:跟你这类似select cid as '编号',T_schedule.schedulecode as '车次',cityname as '路径城市', T_schedule.arrivaltime as '到达时间',
     T_schedule.leavetime as '离开时间',T_schedule_detail.distance as '起始站之本站距离',
     numofschedule as '路径城市顺序',
     T_schedule_detail.days as '起始站到本站天数',fromcity as '起始站', tocity as '终点站',
    T_schedule.leavetime as '起始时间',T_schedule.arrivaltime as '终止时间',traintype,speed
    from T_schedule,T_schedule_detail where T_schedule_detail.schedulecode=T_schedule.schedulecode
      

  8.   

    1.看看flightID、Dcity、Tcity是不是varchar等文本字段类型。
    2.判断TextBox是否填写,未填写时,这部分sql要排除掉,否则like ''查询不到结果。
    3.like是模糊查询关键字,要写成'%...%'格式。如果没有%,就与=相同含义了。
    4.注意用户填写'、%等特殊字符,预先处理一下。            conn.Open();
                StringBuilder sb = new StringBuilder();
                sb.Append("select flightID as 航班号,Dcity as 出发城市,Tcity as 到达城市 from flight");
                if ((!string.IsNullOrEmpty(textBox5.Text)) || (!string.IsNullOrEmpty(textBox1.Text)) || (!string.IsNullOrEmpty(textBox2.Text)))
                {
                    sb.Append(" where ");
                }
                if (!string.IsNullOrEmpty(textBox5.Text))
                {
                    sb.Append(" flightID like '%" + this.textBox5.Text + "%'");
                }
                if (!string.IsNullOrEmpty(textBox1.Text))
                {
                    sb.Append(" Dcity like '%" + this.textBox1.Text + "%'");
                }
                if (!string.IsNullOrEmpty(textBox2.Text))
                {
                    sb.Append(" Tcity like '%" + this.textBox2.Text + "%'");
                }
                string costr = sb.ToString();
                myc = new SqlCommand(costr, conn);
                myda = new DataSet();
                myd = new SqlDataAdapter(myc);
                myd.Fill(myda, "flight");
                this.dataGridView1.DataSource = this.myda.Tables["flight"];
      

  9.   

    我觉得应该是编码的问题 你不能用复制代码然后运行的方式 去试验  你要自己照着代码在sqlms里面写一遍    因为我们在论坛上使用的编码方式和sqlms里面的不同
    我直接把2楼的代码贴上去 也报错  但是我把所有空格去掉然后再重新打出来后运行 就通过了
    而且 你看它抱得错 都是      ' ' 附近有语法错误。   为什么呢  因为这个空格 sql编译器无法识别,为什么不能识别呢 因为两者用的是不同的编码,所以你看上去都是空格,但编译器不能识别
      

  10.   

    16楼给的很详细了。
    建议LZ在出现问题时,注意加断点跟踪,把执行的SQL读取出来放到sqlms里分析,有些东西肉眼是看不出来的。
      

  11.   

    应该是这样的吧:
                    //conn.Open();
                    costr = "select flightID as 航班号,Dcity as 出发城市,tcity as 到达城市 from flight where flightID like'" + this.textBox5.Text + "'" + "and  Dcity like '" + this.textBox1.Text + "'" + "and  Tcity like '" + this.textBox2.Text + "'";
                    //myc = new SqlCommand(costr, conn);
                    myda = new DataSet();
                    myd = new SqlDataAdapter(costr,con);
                    myd.Fill(myda, "flight");
                    this.dataGridView1.DataSource = this.myda.Tables["flight"];
                    //注:在dataSet(数据集)下不需要打开数据库连接.且SqlDataAdapter就该看成
                      //myd=new SqlDataAdapter(sql语句,数据库连接对象)
      

  12.   


    太感谢了 2.判断TextBox是否填写,未填写时,这部分sql要排除掉,否则like ''查询不到结果。正是这一点的原因这么详细  太感谢了
      

  13.   

    string sql1 = "select * from Product where CATALOGID like @CATALOGID";
                cmd = new OleDbCommand(sql1, cn);
                cmd.Parameters.AddWithValue("@CATALOGID", "%" + this.Cc.Value + "%");这样写不会出错