首页 新闻 会员 周边 捐助

这段代码为什么不能正常运行?

0
悬赏园豆:10 [已关闭问题]
<P>我做的是一个新闻搜索页面,根据标题或时间进行查询,因为标题和时间的sql语句不一样,所以我采用了两个sqlDataSource控件,当选择第标题单选项的时候,把sqlDataSource2作为GridView的数据源控件,当选择时间单选项的时候,把sqlDataSource1控件作为GridView的数据源控件,可是GridView中的数据总是为空,前提数据库里是有数据。</P> <P>sqlDataSource2的SelectCommand如下:</P> <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SelectCommand="SELECT [newsId], [title], [author], [fromWhere], [releasedTime] FROM [News] WHERE [title] LIKE <A href="mailto:'%@title%'">'%@title%'</A>"</P> <P>sqlDataSource1的SelectCommand如下:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SelectCommand="SELECT [newsId], [title], [author], [fromWhere], [releasedTime] FROM [News] WHERE [releasedTime] &gt; <A href="mailto:'@releasedTime'">'@releasedTime'</A>"<BR></P> <P>protected void ButtonSearch_Click(object sender, EventArgs e)<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (RadioButtonListSearchBy.SelectedValue == "0")<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GridViewNews.DataSourceID = "SqlDataSource2";&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string selectString = SqlDataSource2.SelectCommand;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GridViewNews.DataBind();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (RadioButtonListSearchBy.SelectedValue == "1")<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GridViewNews.DataSourceID = "SqlDataSource1";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GridViewNews.DataBind();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; }</P> <P>现在我怀疑是不是GridView与sqlDataSource之间的互操作性原理我没有搞清楚,可是它们之间的代码是隐藏的,不可以调试,所以看不到代码的详细执行过程,请高人赐教,谢谢!</P>
爱上编程的主页 爱上编程 | 初学一级 | 园豆:55
提问于:2008-08-23 10:28
< >
分享
其他回答(2)
0
“WHERE [title] LIKE @title” @title=%标题% 我刚做了一个多重搜索,你参考一下(使用了一个小技巧,1=1)。 (除了下面的方法,使用sqldatasource的过滤功能也挺不错,在你的MSDN中输入: ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/dv_vwdcon/html/aff9e910-8460-4c12-a2e0-529dce65ec52.htm) protected string GetSelectCommand() { //SetSearchParams(); StringBuilder sb = new StringBuilder(); if(this.count!=null) sb.Append("SELECT TOP (@Count) FileID,Title,UploadUser,UploadTime,ModifyTime,CatalogID,KeyWords,UploadIP,FilePath,DisplayOrder,Size,FileType,Status FROM [FileInfo]"); else sb.Append("SELECT FileID,Title,UploadUser,UploadTime,ModifyTime,CatalogID,KeyWords,UploadIP,FilePath,DisplayOrder,Size,FileType,Status FROM [FileInfo]"); sb.Append(" WHERE (1=1)"); //} if (!string.IsNullOrEmpty(keyWord)) { if (this.isFullTextSearch) { sb.Append(" AND (Title LIKE @KeyWord OR KeyWords LIKE @KeyWord OR Description LIKE @Keyword)"); } else { sb.Append(" AND (Title LIKE @KeyWord OR KeyWords LIKE @KeyWord)"); } } if (cataID != 0) { sb.Append(" AND (CatalogID=@CatalogID)"); } if (!String.IsNullOrEmpty(fileType)) { sb.Append(" AND (FileType=@FileType)"); } if (upperFileSize != null) { sb.Append(" AND (Size<@UpperFileSize)"); } if (lowerFileSize != null) { sb.Append(" AND (Size>@LowerFileSize)"); } if (upperUploadTime != null) { sb.Append(" AND (UploadTime<@UpperUploadTime)"); } if (lowerUploadTime != null) { sb.Append(" AND (UploadTime>@lowerUploadTime)"); } if (!String.IsNullOrEmpty(userName)) { sb.Append(" AND (UploadUser LIKE @UploadUser)"); } sb.Append(" ORDER BY UploadTime"); sb.Append(" DESC"); return sb.ToString(); }
奇军 | 园豆:265 (菜鸟二级) | 2008-08-23 13:58
0
如果这么写...1=1 就会导致全表扫描...
咸蛋超人 | 园豆:485 (菜鸟二级) | 2008-08-24 14:49
0
调试的时候,先把CommandText拿到查询分析器中查询一下,如有结果则说明sql语句没有问题.那么就是在绑定的问题了.MSDN中查看一下DataGridView的绑定实现.
hackenliu | 园豆:600 (小虾三级) | 2008-08-25 12:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册