asp.net缓存数据用法
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView1();
}
}
/// <summary>
/// 绑定gridview
/// </summary>
public void BindGridView1()
{
DataSet ds = new DataSet();
ds = (DataSet)Cache["MyDataSet"];
if (ds == null)
{
string sql = "select * from tb order by id desc ";
ds = OracleHelper.Query(OracleHelper.ConnString, sql);
Cache["MyDataSet"] = ds;
CacheMsg.Text = "显式创建的数据集";
}
else
{
CacheMsg.Text = "从缓存中检索到的数据集";
}
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
/// <summary>
/// 清除缓存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
Cache.Remove("MyDataSet");
BindGridView1();
}
/// <summary>
/// 插入数据测试 更新后清除缓存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{
Random ra = new Random();
int id = ra.Next(102, 1000);
string sql = "insert into tb(id,address,enabled) values(" + id + ",'发怒了','Y')";
int i = OracleHelper.ExecuteNonQuery(OracleHelper.ConnString, CommandType.Text, sql, null);
if (i > 0)
{
// ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('数据添加成功!缓存已经清除')</script>");
Cache.Remove("MyDataSet");
BindGridView1();
}
}