在会话中存储多个值 [英] Stroring multiple values in session

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

问题描述



在我提出疑问之前,请考虑以下情况:我的网站上的用户拥有一个profileID.与这个profileID相关联的是一些pluginID.例如:User1可能有与其配置文件关联的2、3和5个插件.

当用户登录时,我将用户的profileID存储在会话变量** cod **中.在某个页面上,用户尝试编辑与其个人资料关联的插件.因此,在该页面上,我已经从数据库中检索了这些pluginID.

我已经应用了此代码,但是这仅从数据库中获取最大的pluginID,而不是所有的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();



我试图弄清楚如何在此会话变量中存储多个pluginID?
谢谢

解决方案

Session["edp1"] = Convert.ToInt32(dr1[0]);



上面的行不正确.每次执行循环时,会话中的值都会被覆盖.这就是为什么只剩下最后一个值的原因.您可以存储列表<整数>在会话中存储多个值.您应该看起来像这样:

  while (dr1.Read())
                {
                    plugInList.Add(Convert.ToInt32(dr1 [ 0 ])));
                }
会话[" ] = plugInList; 



您应该尝试使用TryParse而不是Convert,因为它不太容易出错. cod],因此不需要TryParse.

检查null可能是您应该做的事情...

 对象 sessionVar =会话[" ];
如果(sessionVar!= )
{
  自以来,// 也可以使用(int)Session ["cod"]
  // 一个int是分配给Session ["cod"] 
   int  valueOfCod = Convert.ToInt32(sessionVar);

  // 考虑使查询更具可读性" 
  字符串 sql =
    "  +
    "  +
    "  +
    "  +
    "  +
    "  +
    "  +
    "  +
    "  + valueOfCod.ToString()+
    " ;
} 



由于您知道valueOfCod是一个int值,因此可以跳过使用参数的操作.如果会话变量的类型为字符串,则可以使用SqlCommand.Parameters确保对引号和其他相关格式设置内容的正确处理. (并且取决于您在做什么-sql注入)

问候
Espen Harlinn


Hi,

Before I ask my doubt, please consider this scenario that a user on my website have a profileID. With this profileID are associated some pluginID''s. For eg: User1 might have 2, 3 and 5 plugins associated with its profile.

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

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



I was trying to figure out how can I store multiple pluginID''s in this session variable?
Thanks

解决方案

Session["edp1"] = Convert.ToInt32(dr1[0]);



The line above is not correct. Every time loop is executed, value in the session is overwritten. That is why you are having only the last value left. You can store a List< int > in session to store multiple values. Your should look something like this:

while (dr1.Read())
                {
                    plugInList.Add(Convert.ToInt32(dr1[0]));
                }
Session["edp1"] = plugInList;



You should try and use TryParse rather than Convert since it is somewhat less error prone.


d@nish has pinpointed your problem, but I guess you are assigning values of type int to Session["cod"] so there is no need for TryParse.

Checking for null may be something you should do ...

object sessionVar = Session["cod"];
if(sessionVar != null)
{
  // Could use (int)Session["cod"] too since 
  // an int is what was assigned to Session["cod"]
  int valueOfCod = Convert.ToInt32(sessionVar);

  // Consider making the query more "readable"
  string sql = 
    " select " +
    "  plugin_id " +
    " from " +
    "  profiles_plugins " +
    " where " +
    " id= "+
    "  (select id " +
    "    from profiles_plugins " +
    "    where profile_id=" + valueOfCod.ToString() + 
    "  )";
}



Since you know that valueOfCod is an int you can skip using parameters. If the session variable was of type string, you could use SqlCommand.Parameters to ensure proper handling of quotes and other related formatting stuff. (and depending on what you are doing - sql injection)

Regards
Espen Harlinn


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

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