将多个值存储在会话变量中

Cip*_*her 2 .net c# asp.net

在我提出问题之前,请考虑以下情况:我网站上的用户有一个profileID.有一些pluginID与此相关profileID.

例如:User1可能有2个,3个和5个插件与他的个人资料相关联.

当用户登录时,我将profileID用户存储在会话变量中cod.在某个页面上,用户尝试编辑plugins与其个人资料相关联的内容.所以,在那个页面上,我必须pluginID从数据库中检索它们.

我已经应用了这段代码,但这只pluginID取得了DB 的最大值,而不是所有pluginID的.

SqlCommand cmd1 = new SqlCommand("select plugin_id from profiles_plugins where id=(select id from profiles_plugins where profile_id=" + Convert.ToInt32(Session["cod"]) + ")", con);
SqlDataReader dr1 = cmd1.ExecuteReader();
if (dr1.HasRows)
{
  while (dr1.Read())
  {
    Session["edp1"] = Convert.ToInt32(dr1[0]);
  }
}
dr1.Close();
cmd1.Dispose();
Run Code Online (Sandbox Code Playgroud)

我试图找出如何pluginID在此会话变量中存储多个?

谢谢

Tom*_*son 5

首先将值读取到列表或数组,然后存储列表或数组:

using(SqlCommand cmd1 = new SqlCommand("select plugin_id from profiles_plugins where id=(select id from profiles_plugins where profile_id=" + Convert.ToInt32(Session["cod"]) + ")", con))
{

    SqlDataReader dr1 = cmd1.ExecuteReader();
    var yourList = new List<int>();
    if (dr1.HasRows)
    {
        while (dr1.Read())
        {
           yourList.Add(Convert.ToInt32(dr1[0]));
        }
    }
    Session["edp1"] = yourList;
    dr1.Close();
}
Run Code Online (Sandbox Code Playgroud)

另外,请阅读Marc Gravell对您问题的评论.