这个你在前台程序判断一下:sql = 'select * from t where 1= 1 'if 年份 <> 空
   sql = ' and year(日期)= '+ 年份if 月份  <> 空
   sql = ' and year(日期)= '+ 月份       

解决方案 »

  1.   

    根据界面中空间录入是否为空,来拼接响应sql、
    举个例子,比如有两个控件  日期是textBox1  姓名是 textBox2那代码代码大概就是这样子         string sql = "select * from T where 1=1 ";
             if (!string.IsNullOrEmpty(textBox1.Text))
                   sql += "and data = " + textBox1.Text;
             if (!string.IsNullOrEmpty(textBox2.Text))
                   sql += "and name = " + textBox2.Text;
      

  2.   

    对每个变量附上null值,然后用isnull来判断是否存在,比如where year(日期)=isnull(@年份,year(日期))