所以我有一个DataTable使用:
SELECT * FROM People WHERE ID = ?
您可以理解,这只会检索一行,因为ID是唯一的:
usersTableAdapters.UsersTableAdapter user = new usersTableAdapters.UsersTableAdapter();
users.UsersDataTable userDataTable = user.getUserInfo(id);
然后我将DataTable存储到会话中:
HttpContext.Current.Session.Add("userDT", userDataTable);
现在我想弄清楚,如何在会话中从userDT获取特定列?为了更清楚firstname列?
答案 0 :(得分:1)
首先将会话对象强制转换为数据表。
var tbl = ((DataTable)HttpContext.Current.Session["userDT"])
然后像数据表一样使用它:
var col = tbl.Columns["firstName"];
答案 1 :(得分:1)
您必须将Session对象强制转换为users.UsersDataTable: users.UsersDataTable userDataTable = Session [“userDT”] as users.UsersDataTable;
答案 2 :(得分:-1)
试试这个,
DataView dv = new DataView((DataTable)HttpContext.Current.Session["userDT"]);
使用列
获取表格 DataTable dt = dv.ToTable(true, "firstName");