複製代碼 代碼如下:public DataSet createCache()
{
//返回DataSet
DataSet ds=new DataSet();
OleDbConnection conn=new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source="+Server.MapPath("data.mdb"));
conn.Open();
string sql="select * from data1 order by createtime asc";
OleDbDataAdapter cmd=new OleDbDataAdapter(sql,conn);
cmd.Fill(ds);
cmd.Dispose();
conn.Dispose();
return ds;
}
private void Button1_Click(object sender, System.EventArgs e)
{
//清除Cache
Cache.Remove("DataList");
Response.Write("緩衝清除成功");
}
private void Button2_Click(object sender, System.EventArgs e)
{
//建立Cache
if(Cache["DataList"]==null)
{
//緩衝不存在建立緩衝
Cache.Insert("DataList",(DataSet)createCache());
DataSet ds=(DataSet)Cache["DataList"];
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}
else
{
//緩衝存在執行綁定
DataSet ds=(DataSet)Cache["DataList"];
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}
}