日期:2013-06-09 浏览次数:20425 次
else set @SQL='select top '+str(@pageSize) +' * from ( select top '+str(@recordCount - @pageSize * @pageIndex)+' productID,productName, unitPrice from products order by productID desc) TempTable order by productID asc' exec(@SQL)GO好了,存储过程建好了,那么如何在.Net中使用呢?请看以下代码:
private uint pageCount; //总页数 private uint recordCount; //总记录数
private DataSet GetPageData(uint pageSize, uint pageIndex)
{
string strConn = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
SqlCommand command = new SqlCommand("GetCustomDataPage",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);