帮看看这段代码有什么问题
protected void BookSearch_Click(object sender, ImageClickEventArgs e)
{
conn.Open();
string sql = "SELECT book_name,writer,price FROM book WHERE " + " ' " + DDLsearch.SelectedValue + " ' " + "LIKE '@select% ' ";
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
sda.SelectCommand.Parameters.Add(new SqlParameter( "@select ", SqlDbType.NVarChar)).Value = TBBookSearch.Text.Trim();
DataSet ds = new DataSet();
sda.Fill(ds);
GridViewSearch.DataSource = ds;
GridViewSearch.DataBind();
}
这是ImageButton的点击事件,在GridView中显示查询的书,DDLSearch是下拉列表,其各个列的Value值对应着数据库中的列。不知为什么查询不出数据
------解决方案--------------------沙发,帮忙顶
------解决方案--------------------..... "LIKE '@select% ' ".......
-----------
..... "like @select ".....
sda.SelectCommand.Parameters.Add(new SqlParameter( "@select ", SqlDbType.NVarChar)).Value = TBBookSearch.Text.Trim()+ "% ";
------解决方案--------------------跟踪一下吧。
------解决方案--------------------string sql = "SELECT book_name,writer,price FROM book WHERE " + DDLsearch.SelectedValue + " LIKE '@select% ' ";
------解决方案--------------------字段就不要单引号括起来了
string sql = "SELECT book_name,writer,price FROM book WHERE " + DDLsearch.SelectedValue.Trim() + "LIKE '@select% ' ";
------解决方案--------------------不知道,我只知道用参数是我写的那样用的,而不是 '@select% ',我用的都没有什么错误
如果@select只是你 like 参数的一部分那么ADO.NET里的参数也就失去它的意义了
------解决方案--------------------SqlConnection conn = new SqlConnection( "server=.;uid=sa;pwd=sql;database=Northwind; ");
SqlDataAdapter sda = new SqlDataAdapter( "select * from Customers where CompanyName like @p ",conn);
sda.SelectCommand.Parameters.Add( "@p ", "A% ");
DataSet ds = new DataSet();
sda.Fill(ds);
Response.Write(ds.Tables[0].Rows.Count.ToString());
-------------
输出 4
------解决方案--------------------跟踪一下,生成的SQL语句,再到MSSql中查查,哪里出错了