asp.net初学者问一个根据DropDownList的值进行模糊查询的方法。
在按钮Click事件里我是按如下方式写的:
string strSql = "select A, B, C, D, E from TABLE where ";
switch (DropDownList1.Text)
{
case "注册号":
strSql += "A";
break;
case "企业名称":
strSql += "B ";
break;
}
strSql += " like '%' || :VALUE || '%'";
SqlDataSource1.SelectCommand = strSql;
---------------------------------------
对应的HTML Tag里的SqlDataSource
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="select A, B, C, D, E from TABLE where A like '%' || :VALUE || '%'">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="VALUE" PropertyName="Text" />
</SelectParameters>
</asp:SqlDataSource>
-----------------------------------------------
注:是访问Oracle数据库,所以用了||符号。
一个GridView绑定到SqlDataSource1
程序可以运行,查询也OK。
我只想问这种情况下有没有更合理的方式解决这类问题。
就是根据DropDownList的取值,进行不同数据库字段的模糊查询。