测试代码:
1 public partial class Form1 : Form 2 { 3 public Form1() 4 { 5 InitializeComponent(); 6 } 7 8 private void Form1_Load(object sender, EventArgs e) 9 { 10 List<string> list = new List<string>(); 11 list.Add("小明"); 12 list.Add("小红"); 13 list.Add("小李"); 14 this.checkedComboBoxEdit1.Properties.DataSource = list; 15 } 16 17 private void checkedComboBoxEdit1_EditValueChanged(object sender, EventArgs e) 18 { 19 if (this.checkedComboBoxEdit1.Text != "") 20 { 21 this.textEdit1.Text = this.checkedComboBoxEdit1.Text; 22 this.textEdit2.Text = string.Format("select * from tableA where name in ('{0}')", this.checkedComboBoxEdit1.Text).Replace(",","','"); 23 } 24 } 25 }
效果:
对checkedComboBoxEdit的多值进行SQL查询时,查询中使用IN语句,IN里面第一个条件后面的所有条件的前面都会多出一个空格,导致查询不正确
需将空格去掉再传入SQL查询:
Replace(",","','") 改为 Replace(", ","','")