首页 > 代码库 > ADO:DataSet存入缓存Cache中并使用

ADO:DataSet存入缓存Cache中并使用

原文发布时间为:2008-08-01 —— 来源于本人的百度文章 [由搬家工具导入]

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

public partial class Cacheds : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds=(DataSet)Cache.Get("ds_cache");
        if (!IsPostBack)
        {
            if (ds == null)
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["pubsConn"].ConnectionString);
                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = new SqlCommand("select title_id,title,type from titles", conn);
                DataSet cds = new DataSet();
                sda.Fill(cds, "t1");

                cds.ExtendedProperties.Add("ds_t1", DateTime.Now.ToLongTimeString());
                Cache.Insert("ds_cache", cds, null, DateTime.Now.AddMinutes(2), TimeSpan.Zero);

                Response.Write("对象已经从数据库填充并存入Cache");
                GridView1.DataSource = cds.Tables["t1"];
                GridView1.DataBind();
            }

            else
            {
                Response.Write("对象还在缓存Cache中,直接调用");
                GridView1.DataSource = ds.Tables["t1"];
                GridView1.DataBind();
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Cache.Remove("ds_cache");
    }
}

ADO:DataSet存入缓存Cache中并使用