C# 从会话中存储和检索数据表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2288299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Storing and retrieving datatable from session
提问by subash
How to store a datatable in session and to retrieve the values from the session in c#.net?
如何在会话中存储数据表并从 c#.net 中的会话中检索值?
采纳答案by amexn
Add a datatable into session:
将数据表添加到会话中:
DataTable Tissues = new DataTable();
Tissues = dal.returnTissues("TestID", "TestValue");// returnTissues("","") sample function for adding values
Session.Add("Tissues", Tissues);
Retrive that datatable from session:
从会话中检索该数据表:
DataTable Tissues = Session["Tissues"] as DataTable
or
或者
DataTable Tissues = (DataTable)Session["Tissues"];
回答by JMCampos
You can do it like that but storing a DataSet object in Session is not very efficient. If you have a web app with lots of users it will clog your server memory really fast.
您可以这样做,但在 Session 中存储 DataSet 对象效率不高。如果您有一个拥有大量用户的 Web 应用程序,它会非常快地堵塞您的服务器内存。
If you really must do it like that I suggest removing it from the session as soon as you don't need the DataSet.
如果您真的必须这样做,我建议您在不需要 DataSet 时立即将其从会话中删除。
回答by Pieter Germishuys
this is just as a side note, but generally what you want to do is keep size on the Session and ViewState small. I generally just store IDs and small amounts of packets in Session and ViewState.
这只是一个旁注,但通常你想要做的是保持 Session 和 ViewState 的大小很小。我通常只在 Session 和 ViewState 中存储 ID 和少量数据包。
for instance if you want to pass large chunks of data from one page to another, you can store an ID in the querystring and use that ID to either get data from a database or a file.
例如,如果您想将大块数据从一个页面传递到另一个页面,您可以在查询字符串中存储一个 ID,并使用该 ID 从数据库或文件中获取数据。
PS: but like I said, this might be totally unrelated to your query :)
PS:但就像我说的,这可能与您的查询完全无关:)
回答by Prashant Wagh
To store DataTable
in Session:
要存储DataTable
在会话中:
DataTable dtTest = new DataTable();
Session["dtTest"] = dtTest;
To retrieve DataTable
from Session:
DataTable
从会话中检索:
DataTable dt = (DataTable) Session["dtTest"];
回答by Rui Ruivo
A simple solution for a very common problem
一个非常常见的问题的简单解决方案
// DECLARATION
HttpContext context = HttpContext.Current;
DataTable dt_ShoppingBasket = context.Session["Shopping_Basket"] as DataTable;
// TRY TO ADD rows with the info into the DataTable
try
{
// Add new Serial Code into DataTable dt_ShoppingBasket
dt_ShoppingBasket.Rows.Add(new_SerialCode.ToString());
// Assigns new DataTable to Session["Shopping_Basket"]
context.Session["Shopping_Basket"] = dt_ShoppingBasket;
}
catch (Exception)
{
// IF FAIL (EMPTY OR DOESN'T EXIST) -
// Create new Instance,
DataTable dt_ShoppingBasket= new DataTable();
// Add column and Row with the info
dt_ShoppingBasket.Columns.Add("Serial");
dt_ShoppingBasket.Rows.Add(new_SerialCode.ToString());
// Assigns new DataTable to Session["Shopping_Basket"]
context.Session["Shopping_Basket"] = dt_PanierCommande;
}
// PRINT TESTS
DataTable dt_To_Print = context.Session["Shopping_Basket"] as DataTable;
foreach (DataRow row in dt_To_Print.Rows)
{
foreach (var item in row.ItemArray)
{
Debug.WriteLine("DATATABLE IN SESSION: " + item);
}
}