'*' 附近有语法错误。
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Exception: '*' 附近有语法错误。
源错误:
行 853: catch (System.Data.SqlClient.SqlException ex)
行 854: {
行 855: throw new Exception(ex.Message);
行 856: }
行 857: return ds;
|
源文件: E:\bookshop\bookshop\DBUtility\DbHelperSQL.cs 行: 855
堆栈跟踪:
[Exception: '*' 附近有语法错误。] Maticsoft.DBUtility.DbHelperSQL.Query(String SQLString, SqlParameter[] cmdParms) in E:\bookshop\bookshop\DBUtility\DbHelperSQL.cs:855 BookShop.DAL.tb_bookinfo.GetListByKey(String key) in E:\bookshop\bookshop\DAL\MyCode\tb_bookinfo.cs:77 BookShop.BLL.tb_bookinfo.GetBookByKey(String key) in E:\bookshop\bookshop\BLL\MyCode\tb_bookinfo.cs:51 BookShop.Web.search.Page_Load(Object sender, EventArgs e) in E:\bookshop\bookshop\Web\search.aspx.cs:21 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 System.Web.UI.Control.OnLoad(EventArgs e) +91 System.Web.UI.Control.LoadRecursive() +74 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207 |
BLL层方法:
public List<Model.tb_bookinfo> GetBookByKey(string key)
{
//return this.GetBookByPageNumber(pageSize, pageNumber, categoryId, "");
DataSet ds = dal.GetListByKey(key);
return DataTableToList(ds.Tables[0]);
}
DAL层方法:
public DataSet GetListByKey(string key)
{
String sql = "select * from tb_bookinfo where BookName like @key";
return DbHelperSQL.Query(sql, new SqlParameter("@key","%"+key+"%"));
}
表现层方法:
string key = ((TextBox)Page.PreviousPage.Controls[0].FindControl("txtSearch")).Text;
List<Model.tb_bookinfo> searchBooks = new BLL.tb_bookinfo().GetBookByKey(key);
Repeater1.DataSource = searchBooks;
Repeater1.DataBind();
主要目的是实现输入关键词 从tb_bookinfo表的BookName列搜索
你先调试下,看下到底是哪一行出错了.
既然报告*附近,那应该是sql写法有错误.
你先把sql语句放到sql查询分析器里试一下.看能不能执行
是不是少了单引号,改成
return DbHelperSQL.Query(sql, new SqlParameter("@key","'%"+key+"%'"));
现在不现实错误了,可是为什么搜索之后没有显示呢。。
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<ul class="title_ul2">
<li class="title_booklist0" ><a href='<%#Eval("BookId","book.aspx?id={0}") %>'><%#Eval("BookName") %></a></li>
<li class="title_booklist2"><%#Eval("BookAuthor") %></li>
<li class="title_booklist4"><%#Eval("BookPrice","{0:0.00}") %>元</li>
</ul>
</ItemTemplate>
<AlternatingItemTemplate>
<ul class="title_ul3">
<li class="title_booklist0" ><a href='<%#Eval("BookId","book.aspx?id={0}") %>'><%#Eval("BookName") %></a></li>
<li class="title_booklist2"><%#Eval("BookAuthor") %></li>
<li class="title_booklist4"><%#Eval("BookPrice","{0:0.00}") %>元</li>
</ul>
</AlternatingItemTemplate>
</asp:Repeater>
这是repeater绑定
protected void Page_Load(object sender, EventArgs e)
{
if (Page.PreviousPage != null)
{
if (Page.PreviousPage.IsCrossPagePostBack)
{
BLL.tb_bookinfo bookManager = new BLL.tb_bookinfo();
string key = ((TextBox)Page.PreviousPage.Controls[0].FindControl("txtSearch")).Text;
List<Model.tb_bookinfo> searchBooks =bookManager.GetBookByKey(key);
Repeater1.DataSource = searchBooks;
Repeater1.DataBind();
}
}
}
这是后台代码
@Johnson_7: 你调试一下
看看 是否能来到
List<Model.tb_bookinfo> searchBooks =bookManager.GetBookByKey(key);
key 是否是预期的值,并且有 searchBooks 数据返回