private void DataGridDataBind()
{
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
//创建数据适配器对象
SqlDataAdapter da = new SqlDataAdapter("select LastName,FirstName,BirthDate,City from Employees",conn);
//创建DataSet对象
DataSet ds = new DataSet();
try
{
//从指定的索引开始取PageSize条记录
da.Fill(ds,startIndex,dgCustomPage.PageSize,"CurDataTable");
                
//填充数据集
da.Fill(ds,"AllDataTable");
//设置DataGrid控件实际要显示的项数
dgCustomPage.VirtualItemCount = ds.Tables["AllDataTable"].Rows.Count;
//进行数据绑定
dgCustomPage.DataSource = ds.Tables["CurDataTable"];
dgCustomPage.DataBind();
}
catch(Exception error)
{
Response.Write(error.ToString());
}
}
下面这两句,分别做了些什么呢?:
第一句,把指定行数的记录填充到DS中?
第二句,把所有的记录填充条DS中?//1,从指定的索引开始取PageSize条记录
da.Fill(ds,startIndex,dgCustomPage.PageSize,"CurDataTable");
                
//2,填充数据集
da.Fill(ds,"AllDataTable");那么在我绑定的时候 DS 中究竟存了些什么呢?
我只希望每次存当前页的记录,,,没这么做过分页,,,也不晓得该怎么问哪位可以帮忙回个话。

解决方案 »

  1.   

    这段是我从书上抄的性能应该也不怎么样吧它每次都SELECT 了全表完了以后只去了 全表的行数。不理解。
      

  2.   

    没人回复自己回算到 刚才写的一段分页 不用Procedure        public static int AllRowsCount(SqlCommand selectCountCmd)
            {
                int allrowscount = 0;
                try
                {
                    if(selectCountCmd.Connection.State.Equals(ConnectionState.Closed))
                    {
                        selectCountCmd.Connection.Open();
                    }
                    allrowscount = Convert.ToInt32(selectCountCmd.ExecuteScalar());            }
                catch(SqlException sqlex)
                {
                    System.Console.WriteLine(sqlex.Message);
                    allrowscount = 0;
                }
                catch(Exception ex)
                {
                    System.Console.WriteLine(ex.Message);
                    allrowscount = 0;
                }
                finally
                {
                    selectCountCmd.Connection.Close();
                }
                return allrowscount;
            }        public static DataTable CurrentPageData(SqlCommand selectAllRowsCmd,int currentPageIndex,int pageSize)
            {
                DataSet ds = new DataSet();
                try
                {
                    if(selectAllRowsCmd.Connection.State.Equals(ConnectionState.Closed))
                    {
                        selectAllRowsCmd.Connection.Open();    
                    }
                    SqlDataAdapter adapter = new SqlDataAdapter(selectAllRowsCmd);
                    adapter.Fill(ds,pageSize*currentPageIndex,pageSize,"CurrentPageData");
                }
                catch(Exception ex)
                {
                    System.Console.WriteLine(ex.Message);        
                }
                finally
                {
                    selectAllRowsCmd.Connection.Close();
                }
                int i = ds.Tables["CurrentPageData"].Rows.Count;
                return ds.Tables["CurrentPageData"];
            }        public int AllUserCount()
            {
                int allUserCount = 0;
                SqlConnection conn = DbConn.CreateSqlConn();
                SqlCommand cmd = new SqlCommand("select count(*) from users",conn);
                allUserCount = Pagination.AllRowsCount(cmd);
                return allUserCount;
            }        public DataTable UserCurrentPageData(int currentPageIndex,int pageSize)
            {
                DataTable dt = null;
                SqlConnection conn = DbConn.CreateSqlConn();
                SqlCommand cmd = new SqlCommand("select * from users",conn);
                dt = Pagination.CurrentPageData(cmd,currentPageIndex,pageSize);
                return dt;
            }