永发信息网

c#.net 组合查询

答案:1  悬赏:80  手机版
解决时间 2021-02-28 13:20
  • 提问者网友:椧運幽默
  • 2021-02-27 18:20
private void all_select_Click(object sender, EventArgs e) { string time=dateTimePicker1.Value.ToString("d"); string tb1=textBox1.Text.ToString().Trim(); string bm=comboBox1.Text; string cz=comboBox2.Text; string WhereText = "where"; string cmdText = "select * from ck_出库单"; if (checkBox1.Checked==true) { WhereText += "出库时间 like'" + time.Trim() +"'and"; } if (checkBox2.Checked == true) { WhereText += "出库单id ='" + tb1 + "'and"; } if (checkBox3.Checked == true) { WhereText += "领用部门 ='" + bm.Trim() + "'and"; } if (checkBox4.Checked) { WhereText += "操作员 ='" + bm.Trim() + "'and"; } if (WhereText.Length > 0) { cmdText += WhereText.Substring(0, WhereText.Length - 4); } SqlConnection con = new SqlConnection(connetcionString); con.Open(); DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand(cmdText, con); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); dataGridView1.DataSource = ds.Tables[0]; con.Close(); } 帮我看看哪里出了错!!
最佳答案
  • 五星知识达人网友:青尢
  • 2021-02-27 19:45
注意我黑色粗体部分: string cmdText = "select * from ck_ 出库单 "; //“出库单”后面空一格 and 结尾的必须后面加一个空格,在and后面! if (checkBox1.Checked==true) { WhereText += "出库时间 like '%" + time.Trim() +"%' and "; //缺少%,LIKE必须用百分号! } if (checkBox2.Checked == true) { WhereText += "出库单id ='" + tb1 + "' and "; } if (checkBox3.Checked == true) { WhereText += "领用部门 ='" + bm.Trim() + "' and "; } if (checkBox4.Checked) { WhereText += "操作员 ='" + bm.Trim() + "' and "; } WhereText = WhereText.Remove(WhereText.LastIndexOf("and")); //移除最后一个and!
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯