读书人

asp.net菜鸟问一个根据DropDownList的

发布时间: 2011-12-25 23:21:20 作者: rapoo

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的取值,进行不同数据库字段的模糊查询。



[解决办法]
既然字段不固定的话,那么就自动用字符串连接了

可以将表的字段和中文名做一个基础数据表保存起来

在dropdownlist绑定的时候,valuefield用字段名,textfield用中文名

这样通过selectedvaule 就可以直接访问了

读书人网 >C#

热点推荐