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");
这句有异常
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");
这句有异常
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 行: ' ' 附近有语法错误。
如果textbox5用数据 而 textbox1为空 那么就查不出来的
我想你数据库的字段没有为null的把 用 or 可以实现
第 3 行: ' ' 附近有语法错误。
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
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"];
我直接把2楼的代码贴上去 也报错 但是我把所有空格去掉然后再重新打出来后运行 就通过了
而且 你看它抱得错 都是 ' ' 附近有语法错误。 为什么呢 因为这个空格 sql编译器无法识别,为什么不能识别呢 因为两者用的是不同的编码,所以你看上去都是空格,但编译器不能识别
建议LZ在出现问题时,注意加断点跟踪,把执行的SQL读取出来放到sqlms里分析,有些东西肉眼是看不出来的。
//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语句,数据库连接对象)
太感谢了 2.判断TextBox是否填写,未填写时,这部分sql要排除掉,否则like ''查询不到结果。正是这一点的原因这么详细 太感谢了
cmd = new OleDbCommand(sql1, cn);
cmd.Parameters.AddWithValue("@CATALOGID", "%" + this.Cc.Value + "%");这样写不会出错