⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqlcaching.aspx.cs

📁 ASP C#代码实例 适合初学人士学习使用
💻 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.Configuration;
using System.Data.SqlClient;

namespace Example_13_5
{
	/// <summary>
	/// Summary description for SQLCaching.
	/// </summary>
	public class SQLCaching : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataGrid viewStateDG;
		protected System.Web.UI.WebControls.Label DataCachingMsg;
		private readonly string SQLCONNECTIONSTRING = ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"].ToString();
		protected System.Web.UI.WebControls.Button SetProcedure;
		private static Caching dataCaching = Caching.Cache;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!Page.IsPostBack)
			{
				///绑定DataGrid控件的数据
				BindDataGrid();
			}
		}

		private DataSet GetStaffs()
		{
			///从数据库中Staff表获取所有记录
			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()
		{
			///从数据库中Staff表获取所有记录
			SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
			SqlDataAdapter da = new SqlDataAdapter("Pr_GetStaffs",myConnection);

			///设置为执行存储过程
			da.SelectCommand.CommandType = CommandType.StoredProcedure;

			///定义数据集DataSet
			DataSet ds = new DataSet();

			///执行数据库查询
			myConnection.Open();			
			da.Fill(ds,"CachingStaff");

			///关闭数据库的链接
			myConnection.Close();

			///返回数据集ds
			return(ds);			
		}


		private void BindDataGrid()
		{
			DateTime dtStart = DateTime.Now;

			DataSet ds = new DataSet();
			
			switch(dataCaching)
			{
				case Caching.Normal:
				{
					for(int i = 0; i < 100; i++)
					{
						ds = GetStaffs();
					}
					break;
				}
				case Caching.Cache:
				{
					for(int i = 0; i < 100; i++)
					{
						ds = GetCachingStaffs();
					}
					break;
				}
				default:
					ds = GetStaffs();
					break;
			}

			///绑定DataGrid控件的数据
			viewStateDG.DataSource = ds;
			viewStateDG.DataBind();	
	
			DateTime dtEnd = DateTime.Now;
			String timeString = (dtEnd - dtStart).ToString();
			DataCachingMsg.Text += "共消耗时间为:"
				+ timeString.Substring(timeString.LastIndexOf(".") + 1,timeString.Length - timeString.LastIndexOf(".") -1);

		}

		#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.SetProcedure.Click += new System.EventHandler(this.SetProcedure_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void SetProcedure_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 + -