create proc getpage
(
@tablename varchar(255),@pagecount int=1,@pagesize int=99999999
)
as
begin
set nocount on
declare @P1 int
declare @sqlstr varchar(4000)
set @pagecount=(@pagecount-1)*@pagesize+1
set @sqlstr='select * from '+@tableName
exec sp_cursoropen @P1 output,@sqlstr
exec sp_cursorfetch @P1,16,@pagecount,@pagesize
exec sp_cursorclose @P1
end
asp.net代码
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
this.Label1.Text="1";
getpage("person",3,4);
}
}
private void getpage(string tablename,int page,int pagerow)
{
SqlConnection conn=DB.createDB();
SqlCommand cmd=new SqlCommand("getpage",conn);
cmd.CommandType=CommandType.StoredProcedure;
SqlParameter parametertablename=new SqlParameter("@tablename",SqlDbType.VarChar,255);
parametertablename.Value=tablename;
cmd.Parameters.Add(parametertablename);
SqlParameter parameterpage=new SqlParameter("@pagecount",SqlDbType.Int,4);
parameterpage.Value=page;
cmd.Parameters.Add(parameterpage);
SqlParameter parameterpagerow=new SqlParameter("@pagesize",SqlDbType.Int,4);
parameterpagerow.Value=pagerow;
cmd.Parameters.Add(parameterpagerow);
try
{
conn.Open();
}
catch(Exception ex)
{
throw new Exception("打开数据库失败",ex);
}
SqlDataReader sdr=null;
try
{
sdr=cmd.ExecuteReader();
this.DataGrid1.DataSource=sdr;
this.DataGrid1.DataBind();
}
catch(Exception ex)
{
throw new Exception(ex.Message,ex);
}
finally
{
conn.Close();
sdr.Close();
}
}
请客位大侠帮我看看是什么原因