日期:2014-05-18 浏览次数:20431 次
private static string ConStr = ConfigurationManager.ConnectionStrings["ConnectionNorthwind"].ConnectionString; double _totalPages; Int32 _currentPageNumber = 1; //以下两种的存储过程数据表中的数据都是万级别的,各种方法的效率差别不是很大 protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindDataReturn(_currentPageNumber); // BindData(_currentPageNumber); } } //调用采用临时表分页的存储过程 private void BindData(int pageIndex) { using (SqlConnection Con = new SqlConnection(ConStr)) { using (SqlCommand Cmd = new SqlCommand("Get_Customers_By_Page", Con)) { Cmd.CommandType = CommandType.StoredProcedure; Cmd.Parameters.AddWithValue("@CurrentPage", pageIndex); Cmd.Parameters.AddWithValue("@PageSize", gvCustomer.PageSize); SqlParameter ParmTotal = new SqlParameter("@TotalRecords", SqlDbType.Int); //获取存储过程中的输出参数 ParmTotal.Direction = ParameterDirection.Output; Cmd.Parameters.Add(ParmTotal); //Cmd.Parameters.Add(new SqlParameter("@TotalRecords",SqlDbType.Int)).Direction = ParameterDirection.Output; Con.Open(); gvCustomer.DataSource = Cmd.ExecuteReader(CommandBehavior.CloseConnection); gvCustomer.DataBind(); CurrentPage.Text = _currentPageNumber.ToString(); if (!Page.IsPostBack) { //必须关闭DR才能正确的获取输出参数的值 int Total = ((Int32)Cmd.Parameters["@TotalRecords"].Value); _totalPages = Total / ((int)gvCustomer.PageSize); TotalPages.Text = (System.Math.Ceiling(_totalPages)).ToString(); } if (_currentPageNumber == 1) { PreviousPage.Enabled = false; if (_totalPages > 1) { NextPage.Enabled = true; } else { NextPage.Enabled = false; } } else { PreviousPage.Enabled = true; if (_currentPageNumber == _totalPages) { NextPage.Enabled = false; } else { NextPage.Enabled = true; }