日期:2014-05-18  浏览次数:20562 次

gridView模糊查询,
搜索条件两个,一个年份,一个建立者。建立者是模糊查询。
我想不输入建立者,只输入年份,能找到当年所有的记录。
现在问题是:
必须输入年份和建立者才能搜索到记录。

另:
该sql语句在数据库中执行没有问题。
select * from pubCostVersion where yearly=2007 and createby like '%%'

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
  <Columns>
  <asp:BoundField DataField="Yearly" HeaderText="Yearly" SortExpression="Yearly" />
  <asp:BoundField DataField="Monthly" HeaderText="Monthly" SortExpression="Monthly" />
  <asp:CheckBoxField DataField="isCale" HeaderText="isCale" SortExpression="isCale" />
  <asp:BoundField DataField="CreateBy" HeaderText="CreateBy" SortExpression="CreateBy" />
  <asp:BoundField DataField="CreateTime" HeaderText="CreateTime" SortExpression="CreateTime" />
  <asp:BoundField DataField="ModifyBy" HeaderText="ModifyBy" SortExpression="ModifyBy" />
  <asp:BoundField DataField="ModiFyTime" HeaderText="ModiFyTime" SortExpression="ModiFyTime" />
  </Columns>
  </asp:GridView>
  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Bw_rental_testConnectionString %>"
  SelectCommand="SELECT [Yearly], [Monthly], [isCale], [CreateBy], [CreateTime], [ModifyBy], [ModiFyTime] FROM [pubCostVersion] WHERE (([Yearly] = @Yearly) AND ([CreateBy] LIKE '%' + @CreateBy + '%'))">
  <SelectParameters>
  <asp:ControlParameter ControlID="yearly" DefaultValue="2007" Name="Yearly" PropertyName="Text" Type="Int32" />
  <asp:ControlParameter ControlID="TextBox1" Name="CreateBy" PropertyName="Text" Type="String" />
  </SelectParameters>
  </asp:SqlDataSource>




------解决方案--------------------
code=SQL]select * from pubCostVersion where (yearly=null or yearly=2007) and (createby like '%% ' or createby =null)[[/code]

------解决方案--------------------
SQL code


select * from pubCostVersion where (yearly=null or yearly=2007) and  (createby like   '%%  ' or createby =null)

------解决方案--------------------
不用那么麻烦.你用like就行了:
SelectCommand="SELECT [Yearly], [Monthly], [isCale], [CreateBy], [CreateTime], [ModifyBy], [ModiFyTime] FROM [pubCostVersion] WHERE (([Yearly] like '%"+@Yearly+"%') AND (([CreateBy] LIKE '% " + @CreateBy + "% ')))"
------解决方案--------------------
SelectCommand="SELECT [Yearly], [Monthly], [isCale], [CreateBy], [CreateTime], [ModifyBy], [ModiFyTime] FROM [pubCostVersion] WHERE (([Yearly] = @Yearly) AND ((@CreateBy = '') or ([CreateBy] LIKE '% ' + @CreateBy + '% ')))" >