📄 adonetcaching.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace Example_13_4
{
/// <summary>
/// Summary description for ADONETCaching.
/// </summary>
public class ADONETCaching : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid viewStateDG;
private readonly string SQLCONNECTIONSTRING = ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"].ToString();
protected System.Web.UI.WebControls.Button SetDataCaching;
protected System.Web.UI.WebControls.Label DataCachingMsg;
private static Caching dataCaching = Caching.Cache;
private int nCurrentPageIndex = 0;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
///绑定DataGrid控件的数据
BindDataGrid();
}
}
private DataSet GetStaffs()
{
///从数据库中Tabs表获取所有记录
String cmdText = "SELECT * FROM Staff ORDER BY Staff_ID";
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlDataAdapter da = new SqlDataAdapter(cmdText,myConnection);
///定义数据集DataSet
DataSet ds = new DataSet();
///执行数据库查询
myConnection.Open();
da.Fill(ds,"MyStaff");
///关闭数据库的链接
myConnection.Close();
///返回数据集ds
return(ds);
}
private DataSet GetCachingStaffs(int nStartIndex,int nMaxIndex)
{
///从数据库中Tabs表获取所有记录
String cmdText = "SELECT * FROM Staff ORDER BY Staff_ID";
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
String countText = "SELECT COUNT(*) AS CountStaff FROM Staff";
SqlCommand myCommand = new SqlCommand(countText,myConnection);
SqlDataAdapter da = new SqlDataAdapter(cmdText,myConnection);
///定义数据集DataSet
DataSet ds = new DataSet();
///执行数据库查询
myConnection.Open();
int nMaxCount = Int32.Parse(myCommand.ExecuteScalar().ToString());
int nPageCount = nMaxCount / viewStateDG.PageSize;
if(viewStateDG.PageSize * nPageCount < nMaxCount)
{
nPageCount++;
}
da.Fill(ds,nStartIndex,nMaxIndex,"CachingStaff");
///关闭数据库的链接
myConnection.Close();
///返回数据集ds
return(ds);
}
private void BindDataGrid()
{
DataSet ds = new DataSet();
switch(dataCaching)
{
case Caching.Normal:
{
ds = GetStaffs();
break;
}
case Caching.Cache:
{
if(viewStateDG.CurrentPageIndex == 0)
{
ds = GetCachingStaffs(0,viewStateDG.PageSize);
}
else
{
if(nCurrentPageIndex > 1)
{
ds = GetCachingStaffs((nCurrentPageIndex-1) * viewStateDG.PageSize,viewStateDG.PageSize);
}
}
break;
}
default:
ds = GetStaffs();
break;
}
if(ds != null && ds.Tables.Count > 0)
{
///绑定DataGrid控件的数据
viewStateDG.DataSource = ds;
viewStateDG.DataBind();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.viewStateDG.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.viewStateDG_PageIndexChanged);
this.SetDataCaching.Click += new System.EventHandler(this.SetDataCaching_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void SetDataCaching_Click(object sender, System.EventArgs e)
{
if(dataCaching == Caching.Normal)
{
dataCaching = Caching.Cache;
DataCachingMsg.Text = "控件DataGrid使用数据缓冲!";
}
else
{
dataCaching = Caching.Normal;
DataCachingMsg.Text = "控件DataGrid没有使用数据缓冲!";
}
///重新绑定DataGrid控件的数据
BindDataGrid();
}
private void viewStateDG_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
viewStateDG.CurrentPageIndex = e.NewPageIndex;
BindDataGrid();
}
}
public enum Caching {Normal,Cache};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -