日期:2014-05-19  浏览次数:20622 次

combobox绑定后取值的问题
我手动绑定了两个combobox,3和4,一个的ITEMS从1到12,一个从1-31,代表月份和日期,但是如下语句却查询不到数据:
select   *   from   person   inner   join   Department   on   person.cDept_num=Department.cDepCode   where   month(person.dBirthDate)= ' "   +comboBox3.SelectedItem.ToString()   +   " '   and   day(person.dBirthDate)= ' "   +comboBox4.SelectedItem.ToString()+   " '

select   *   from   person   inner   join   Department   on   person.cDept_num=Department.cDepCode  
where   month(person.dBirthDate)= '7 '
and   day(person.dBirthDate)= '4 '     却能查询到数据(数据库中有一个dBirthDate为2007-7-4的记录)

------解决方案--------------------
不要 把精力 放在这2行代码上
我测试过了 是正确的!!
错误原因 应该在 你没有贴出来的代码上面 :
=============
SqlConnection conn = new SqlConnection( "Server=.;Database=testManage;Integrated Security=SSPI ");
string str = "select * from stuInfo where month(stuInfo.FBirDate)= ' "+cobShow.SelectedItem.ToString()+ " ' and day(stuInfo.FBirDate)= ' "+cb2.SelectedItem.ToString()+ " ' ";
SqlDataAdapter da = new SqlDataAdapter(str,conn);
DataSet ds = new DataSet();
da.Fill(ds);

MessageBox.Show(ds.Tables[0].Rows.Count.ToString());
------解决方案--------------------
手动输入的这样做应该可以的,这样试下

comboBox1.SelectedItem.ToString().Trim().Replace( " ' ", " ")

不行就断点跟踪,看下你取到的值是什么