asp.net中怎样将缓存中的数据保存到数据库中?
2个回答
展开全部
DataTable dt = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
if (Cache["Products"] != null)
dt = Cache["Products"] as DataTable;
if (dt != null)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void btnInsert(object sender, EventArgs e)
{
if (Cache["Products"] != null)
dt = Cache["Products"] as DataTable;
//向dt加一条记录
//更新缓存
Cache["Products"] = dt;
BindGrid();
}
protected void btnDelete(object sender, EventArgs e)
{
if (Cache["Products"] != null)
dt = Cache["Products"] as DataTable;
//向dt加一条记录
foreach (DataRow row in dt)
{
//假设删除编号为3
if (Convert.ToInt32( row["id"]) == 3)
{
dt.Rows.Remove(row);
}
}
//更新缓存
Cache["Products"] = dt;
BindGrid();
}
protected void btnSave(object sender, EventArgs e)
{
if (Cache["Products"] != null)
dt = Cache["Products"] as DataTable;
//把dt更新入库,可以遍历dt,逐行添加
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
if (Cache["Products"] != null)
dt = Cache["Products"] as DataTable;
if (dt != null)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void btnInsert(object sender, EventArgs e)
{
if (Cache["Products"] != null)
dt = Cache["Products"] as DataTable;
//向dt加一条记录
//更新缓存
Cache["Products"] = dt;
BindGrid();
}
protected void btnDelete(object sender, EventArgs e)
{
if (Cache["Products"] != null)
dt = Cache["Products"] as DataTable;
//向dt加一条记录
foreach (DataRow row in dt)
{
//假设删除编号为3
if (Convert.ToInt32( row["id"]) == 3)
{
dt.Rows.Remove(row);
}
}
//更新缓存
Cache["Products"] = dt;
BindGrid();
}
protected void btnSave(object sender, EventArgs e)
{
if (Cache["Products"] != null)
dt = Cache["Products"] as DataTable;
//把dt更新入库,可以遍历dt,逐行添加
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |