
案例6:图书覆家模块设计 一、任务场最 某网上书店需设计一模块图书搜索模块,该模块完成图书按“关键字“、“出版时何”及“价 格”中一个域多个条件对数据库进行模糊查询,并将查询到的结果分真最示出米,每项最示 5条记录。在第一次浏览该页面或者用户没有输入任何箭选条件的时候,将分页显示所有图 书列表。 二。任务分析及实现 图书搜索界面的实现原理与图书列表页差不多,贝是将排序功能换成了查诲功能。本功能模 块实现时最大的困难就是查询字符串的拼凑及传递。由于这是个多重条作的模物查询,我们 很难直接定文出select语句,所以另外定义了一个方法 Seare城eyWord(stringtitle,stringpublishdate,stringprice)根据用户填写的标题关键 字,选择的出版日期及价格的范围米拼凑出sel0ct语句,注意,这里的h1 ishdate限rc 不是番数据库表中的出版日期及价格字授,这两个参数是一个字符串表达式类似: "UnitPrice>=100andlnitPrice<=200". Searc城eyWord(stringtitle,stringpublishdate,stringprice)方法返回的select语句将 被存储到一个对象变量中,再梭BindData()方法所使用,最终显示查询结果。 Bindata0方法相关代码如下: protectedvoidBindata() stringsqlstr=string.Eapty: SqlConnectionconn=newSqlConnection (e"DataSource=,\SQLEXPRESS:AttachDbFilename=] DataDirectory|\MyBookShop.ndf:IntegratedSecurity"True:UserInstance"True); SqlDataAdapterap=newSqlDataAdapter (VievState["Searchord"].ToString().conn): DataSetds=nevDataSet () ap.Fill(ds."Books): PagedDataSourcehookobject=nesPagedDataSource(: bookobject.DataSource=ds.Tables["Books"].DefaultViev: bookobject.AllonPaging=true: bookobject.PageSize=5:
案例 6:图书搜索模块设计 一、任务场景: 某网上书店需设计一模块图书搜索模块,该模块完成图书按“关键字”、“出版时间”及“价 格”中一个或多个条件对数据库进行模糊查询,并将查询到的结果分页显示出来,每页显示 5 条记录。在第一次浏览该页面或者用户没有输入任何筛选条件的时候,将分页显示所有图 书列表。 二、任务分析及实现: 图书搜索界面的实现原理与图书列表页差不多,只是将排序功能换成了查询功能。本功能模 块实现时最大的困难就是查询字符串的拼凑及传递。由于这是个多重条件的模糊查询,我们 很难直接定义出 select 语句,所以另外定义了一个方法 SearchKeyWord(stringtitle,stringpublishdate,stringprice)根据用户填写的标题关键 字,选择的出版日期及价格的范围来拼凑出 select 语句。注意,这里的 publishdate 跟 price 不是指数据库表中的出版日期及价格字段,这两个参数是一个字符串表达式类似: "UnitPrice>=100andUnitPrice<=200"。 SearchKeyWord(stringtitle,stringpublishdate,stringprice)方法返回的 select 语句将 被存储到一个对象变量中,再被 BindData()方法所使用,最终显示查询结果。 Bindata()方法相关代码如下: protectedvoidBindata() { stringsqlstr=string.Empty; SqlConnectionconn=newSqlConnection(@"DataSource=.\SQLEXPRESS;AttachDbFilename=| DataDirectory|\MyBookShop.mdf;IntegratedSecurity=True;UserInstance=True"); SqlDataAdapterap=newSqlDataAdapter(ViewState["SearchWord"].ToString(),conn); DataSetds=newDataSet(); ap.Fill(ds,"Books"); PagedDataSourcebookobject=newPagedDataSource(); bookobject.DataSource=ds.Tables["Books"].DefaultView; bookobject.AllowPaging=true; bookobject.PageSize=5;

bookobject.CurrentPageIndex=int.Parse (ViewState["Page"].ToStringO): this.LabelIndex.Text=(bookobject.CurrentPagelndex+1).ToString(): this.LabelCount.Text=(hookobject.PageCount).ToString(); SetEnableBut (bookob ject): this.Datal.istBooks.DataSource=bookobject: this.DataListBooks.DataBind(); Searc域eyWord(stringtitle,stringpublishdate,.stringprice)方法相关代到如下: publicstringSearchKeyWord(stringtitle,stringpublishdate,stringprice) stringsglstr=string.Empty: if(title--") if (publishdate=s"") if(price==") sqlstr="select*fronBooks": else sqlstr="select*fronBooksshere"+price: else if(price=-"") sqlstr="select*froaBookswhere"+publishdate:
bookobject.CurrentPageIndex=int.Parse(ViewState["Page"].ToString()); this.LabelIndex.Text=(bookobject.CurrentPageIndex+1).ToString(); this.LabelCount.Text=(bookobject.PageCount).ToString(); SetEnableBut(bookobject); this.DataListBooks.DataSource=bookobject; this.DataListBooks.DataBind(); } SearchKeyWord(stringtitle,stringpublishdate,stringprice)方法相关代码如下: publicstringSearchKeyWord(stringtitle,stringpublishdate,stringprice) { stringsqlstr=string.Empty; if(title=="") { if(publishdate=="") { if(price=="") { sqlstr="select*fromBooks"; } else { sqlstr="select*fromBookswhere"+price; } } else { if(price=="") { sqlstr="select*fromBookswhere"+publishdate;

else sqlstr="select*froaBookswbere"+publishdate+"and"+price: else 1f(publishdate=“) if(price==") sqlstr="select*fronBooksshereTitlelike'%"+title+"s'": else sqlstr='select*fronBookswbereTitlelike'%"+title+"s'+and"+price: else 1f(pric0=”) sqlstr-"select*froaBookswbereTitlelike'%"+titlet"s'+and"tpublishdate: else sqlstr="select*frcaBookssbereTitlelike'%"+title+"s'"+and"-publishdate+"and"+pr ice;
} else { sqlstr="select*fromBookswhere"+publishdate+"and"+price; } } } else{ if(publishdate=="") { if(price=="") { sqlstr="select*fromBookswhereTitlelike'%"+title+"%'"; } else { sqlstr="select*fromBookswhereTitlelike'%"+title+"%'"+"and"+price; } } else { if(price=="") { sqlstr="select*fromBookswhereTitlelike'%"+title+"%'"+"and"+publishdate; } else { sqlstr="select*fromBookswhereTitlelike'%"+title+"%'"+"and"+publishdate+"and"+pr ice;

returnsqlstr:
} } } returnsqlstr; }