[英]Asp.net Caching Issue
我已經緩存了具有“ StoreId”列的數據集。 當我想將數據集導出到Excel時,我想從數據集和Exprot中刪除“ 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”列,我無法弄清楚我在哪里做錯了。 請幫我。
提前致謝。
如果您從不修改放置在Cache中的對象,將使您的生活更加輕松。 從DataSet中刪除列不是線程安全的,因此,如果多個請求同時訪問Cache,則會遇到麻煩。
在這種情況下,我將創建數據集的副本並導出該副本。 為此,請使用DataSet.Copy
方法:
DataSet dsStores = ((DataSet)HttpContext.Current.Cache["stores"]).Copy()
或者找到一種不需要修改DataSet
的導出方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.