在会话变量中存储多个值 [英] Store multiple values in a session variable

查看:126
本文介绍了在会话变量中存储多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我问我的问题,请考虑以下情况:在我的网站用户有一个配置文件ID 。有一些 pluginID 的与此相关的配置文件ID

Before I ask my question, please consider this scenario: a user on my website has a profileID. There are some pluginID's associated with this profileID.

例如:用户1 可能有2个,3个和5个与他的个人资料相关的插件

E.g.: User1 might have 2, 3 and 5 plugins associated with his profile.

当用户登录时,我保存配置文件ID 用户在会话变量 COD 。在某个页面上,用户尝试编辑插件与他的个人资料相关联。因此,在该网页上,我一定要找回那些 pluginID 的从数据库。

When the user logs in, I store the profileID of the user in a session variable cod. ON a certain page, the user tries to edit the plugins associated with his profile. So, on that page, I have to retrieve those pluginID's from the DB.

我已经应用这个code,但这种只获取最大 pluginID 从数据库中,而不是所有的 pluginID 的。

I have applied this code but this fetches only the maximum pluginID from the DB and not all the pluginID's.

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();

我试图找出我怎么可以存储多个 pluginID 的这个会话变量?

感谢

推荐答案

读取值列表或数组,然后再存储列表或数组:

Read the value to a list or array first and then store the list or array:

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();
}

此外,阅读马克Gravell的在你的问题。

Also, read Marc Gravell's comment on your question.

这篇关于在会话变量中存储多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆