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

gridview显示数据的sql语句问题
功能是查询,有一个dropdownlist--DDLSearch,textbox--TBSearch
现在textbox中输入关键字,dropdowlist选择相应的值,比如bookname等,实现分类查询,语句如下
string   sql   =   "SELECT   book_name,writer,price   FROM   book   WHERE   "   +   " ' "   +   DDLsearch.SelectedValue.Trim()   +   " ' "   +   "LIKE   @insert1 ";
若将DDLsearch.SelectedValue.Trim()   改为bookname等是可以实现查询的,但用它后却什么也查不到,为什么?
两个变量不能同时应用吗?
怎样实现类似功能的查询?
不用为dropdownlist的value分别写几个事件吧?



------解决方案--------------------
string sql = "SELECT book_name,writer,price FROM book WHERE " + " ' " + DDLsearch.SelectedValue.Trim() + " ' " + "LIKE @insert1 ";
语句有问题吧:
string sql = "SELECT book_name,writer,price FROM book WHERE " + DDLsearch.SelectedValue.Trim() + "LIKE @insert1 ";

DDLsearch.SelectedValue.Trim()是不是表中的字段名啊


------解决方案--------------------
你的SQL要这样写:
Search tbsearch 是数据库字段名称
string sql = "select * from book where Search = ' "+DDLsearch.SelectedValue.Trim()+ " ' and tbsearch like '% "+TBSearch.text.tostring()+ "% ' ";

------解决方案--------------------
如果DDLsearch.SelectedValue.Trim()是字段名的话,应该用以下这种写法:
string sql = "SELECT book_name,writer,price FROM book WHERE ' " + DDLsearch.SelectedValue.Trim() + " ' LIKE '% " + @insert1 + " ' ";


------解决方案--------------------
上面写错了,不好意思,应该这样:
如果DDLsearch.SelectedValue.Trim()是字段名的话,应该用以下这种写法:
string sql = "SELECT book_name,writer,price FROM book WHERE " + DDLsearch.SelectedValue.Trim() + " LIKE '% " + @insert1 + " ' ";
------解决方案--------------------
为什么又用拼接字符串,又用参数,这种写法超不规范
------解决方案--------------------
caowenkai() 正解
------解决方案--------------------
字段名不需要引号,去掉引号就可以了。

string sql = "SELECT book_name,writer,price FROM book "
+ " WHERE " + DDLsearch.SelectedValue.Trim() + "LIKE @insert1 ";