Asp.net缓存问题

本文关键字:问题 缓存 net Asp | 更新日期: 2023-09-27 18:10:28

我缓存了一个具有"StoreId"列的数据集。当我想导出数据集到Excel,我想从数据集和导出中删除"StoreId"列。

下面是删除和导出到Excel的代码。

if (HttpContext.Current.Cache["stores"] != null)
        {
            using (DataSet dsStores = (DataSet)HttpContext.Current.Cache["stores"])
            {
                if (TrainingUtil.isDataSetValid(dsStores))
                {
                    DataTable dt = dsStores.Tables[0];
                    dt.Columns.Remove("storeId");
                    Quality.Qulaity_Utility.ExportDataSet(dt, ddlCity.SelectedItem.Text.ToString() + "_StoreCodes");
                }
            }
        }
  public static void ExportDataSet(DataTable dt,string filename)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.ContentType = "application/vnd.xls";
        HttpContext.Current.Response.Charset = "";
        HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + filename.Replace(" ", "_").ToString() + ".xls");
        DataGrid dgRecord = new DataGrid();
        //Color Setttings
        dgRecord.HeaderStyle.BackColor = System.Drawing.Color.Cyan;
        dgRecord.DataSource = dt;
        dgRecord.DataBind();
        //Cells color settings
        foreach (DataGridItem dgi in dgRecord.Items)
        {
            foreach (TableCell tcGridCells in dgi.Cells)
            {
                tcGridCells.Attributes.Add("class", "sborder");
            }
        }
        //Render the datagrid
        StringWriter stringWriter = new StringWriter();
        HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
        dgRecord.RenderControl(htmlTextWriter);
        //lstMonthlyReport.RenderControl(htmlTextWriter);
        //Add the style sheet class here
        HttpContext.Current.Response.Write(@"<style> .sborder { color : Black;border : 1px Solid Black; } </style> ");
        //Export
        HttpContext.Current.Response.Write(stringWriter.ToString());
        //End
        HttpContext.Current.Response.End();
        //style to format numbers to string
        //string style = @"<style> body { mso-number-format:'@; } </style>";
    }
}

导出数据后,当我再次想从缓存数据集存储信息我找不到StoreId栏,我不知道我哪里做错了。请帮帮我。

Asp.net缓存问题

如果您从不修改放入缓存中的对象,将使您的工作更轻松。从数据集中删除列不是线程安全的,所以如果多个请求并发访问缓存,你就有麻烦了。

在本例中,我将创建DataSet的克隆并导出该克隆。为此,使用DataSet.Copy方法:

DataSet dsStores = ((DataSet)HttpContext.Current.Cache["stores"]).Copy()

或者找到一种不需要修改DataSet的导出方法