我创建了一个网站,我必须在其中维护数据表&内存中的一些变量。数据表或varibales中的数据/值将根据记录的用户而不同。如何维护?
答案 0 :(得分:3)
您可以将其另存为
DataTable dt = new DataTable();
// I assumed that dt has some data
Session["dataTable"] = dt; // Saving datatable to Session
// Retrieving
DataTable dtt = (DataTable) Session["dataTable"]; // Cast it to DataTable
int a = 43432;
Session["a"] = a;
// Retrieving
int aa = (int) Session["a"];
// classes
class MyClass
{
public int a { get; set; }
public int b { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
MyClass obj = new MyClass();
obj.a = 5;
obj.b = 20;
Session["myclass"] = obj; // Save the class object in Session
// Retrieving
MyClass obj1 = new MyClass();
obj1 = (MyClass) Session["myclass"]; // Remember casting the object
}
答案 1 :(得分:2)
使用带数据表的Session的完美方式是....
public class clsSession
{
public static DataTable dtEmp
{
get
{
if (HttpContext.Current.Session["dtEmp"] != null)
{
return (DataTable)HttpContext.Current.Session["dtEmp"];
}
else
return new DataTable();
}
set
{
HttpContext.Current.Session["dtEmpAddres"] = value;
}
}
}
您可以将数据表存储为clsSession.dtEmp = new DataTable();