请问用存储过程的代码中,如何在SQL语句里加一个where部分的子句?(有现成代码)

bobwang2 2009-03-25 06:15:33
小弟是新手,前一阵把以前写的DataGrid旧代码改成了用存储过程的方式分页,主要代码如下


--@PageSize:分页大小,PageIndex:页号,@PageCount:总页数,@recordCount:记录数
CREATE PROCEDURE GetDataPage_Misc @pageSize int, @pageIndex int, @pageCount int output, @recordCount int output AS
declare @SQL varchar(1000)
select @recordCount=count(*) from Misc
set @pageCount=ceiling(@recordCount*1.0/@pageSize)
if @pageIndex = 0 or @pageCount<=1
set @SQL='select top '+str(@pageSize)+' * from Misc order by MID desc'
else if @pageIndex = @pageCount -1
set @SQL='select * from ( select top '+str(@recordCount - @pageSize * @pageIndex)+' * from Misc order by MID asc) TempTable order by MID desc'
else set @SQL='select top '+str(@pageSize) +' * from ( select top '+str(@recordCount - @pageSize * @pageIndex)+' * from Misc order by MID asc) TempTable order by MID desc'
exec(@SQL)
GO




private DataSet GetPageData(uint pageSize, uint pageIndex)
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["Conn"]);
conn.Open();
SqlCommand command = new SqlCommand("GetDataPage_Misc", conn);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@pageSize", SqlDbType.Int);
command.Parameters["@pageSize"].Value = pageSize;
command.Parameters.Add("@pageIndex", SqlDbType.Int);
command.Parameters["@pageIndex"].Value = pageIndex;
command.Parameters.Add("@pageCount", SqlDbType.Int);
command.Parameters["@pageCount"].Value = pageCount;
command.Parameters["@pageCount"].Direction = ParameterDirection.Output;
command.Parameters.Add("@recordCount", SqlDbType.Int);
command.Parameters["@recordCount"].Value = recordCount;
command.Parameters["@recordCount"].Direction = ParameterDirection.Output;
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds);
pageCount = Convert.ToUInt32(command.Parameters["@pageCount"].Value);
recordCount = Convert.ToUInt32(command.Parameters["@recordCount"].Value);
conn.Close();
return ds;
}

protected void BindDataGrid()
{
DataSet ds = GetPageData((uint)DataGrid1.PageSize, (uint)DataGrid1.CurrentPageIndex);
DataGrid1.VirtualItemCount = (int)recordCount;
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}


这段代码已经在工作了,感觉数据量大的时候果然效率高很多
现在有一新的问题需要帮助,在select * from tablename后面需要加一个where sid = 一个名叫sid的变量
这个sid由网页link后带的参数取得——

int sid = Convert.ToInt32(Request.QueryString["sid"]);


现在我不知道这个where子句应该加在什么地方,请帮忙指出!
改的时候用颜色区分一下,谢谢!
...全文
62 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
peterqiu2 2009-03-27
  • 打赏
  • 举报
回复

CREATE PROCEDURE GetDataPage_Misc @pageSize int, @pageIndex int, @sid int, @pageCount int output, @recordCount int output AS
......
--在所有的查询语句中加where sid = str(@sid)



......
command.Parameters.Add("@sid", SqlDbType.Int);
command.Parameters["@sid"].Value = sid;
......


就这么简单
liu4545533 2009-03-25
  • 打赏
  • 举报
回复
其实一句sql代码就可以实现分页了 没有必要这么麻烦
select top m from tablename where id not in(select top 前面页数*每页数 id from tablename)

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧