Session 操作类

  1     /// <summary>
  2     /// Session 操作类
  3     /// 1、GetSession(string name)根据session名获取session对象
  4     /// 2、SetSession(string name, object val)设置session
  5     /// </summary>
  6     public class SessionHelper
  7     {
  8         /// <summary>
  9         /// 根据session名获取session对象
 10         /// </summary>
 11         /// <param name="name"></param>
 12         /// <returns></returns>
 13         public static object GetSession(string name)
 14         {
 15             return HttpContext.Current.Session[name];
 16         }
 17 
 18         /// <summary>
 19         /// 设置session
 20         /// </summary>
 21         /// <param name="name">session 名</param>
 22         /// <param name="val">session 值</param>
 23         public static void SetSession(string name, object val)
 24         {
 25             HttpContext.Current.Session.Remove(name);
 26             HttpContext.Current.Session.Add(name, val);
 27         }
 28 
 29         /// <summary>
 30         /// 添加Session,调动有效期为20分钟
 31         /// </summary>
 32         /// <param name="strSessionName">Session对象名称</param>
 33         /// <param name="strValue">Session值</param>
 34         public static void Add(string strSessionName, string strValue)
 35         {
 36             HttpContext.Current.Session[strSessionName] = strValue;
 37             HttpContext.Current.Session.Timeout = 20;
 38         }
 39 
 40         /// <summary>
 41         /// 添加Session,调动有效期为20分钟
 42         /// </summary>
 43         /// <param name="strSessionName">Session对象名称</param>
 44         /// <param name="strValues">Session值数组</param>
 45         public static void Adds(string strSessionName, string[] strValues)
 46         {
 47             HttpContext.Current.Session[strSessionName] = strValues;
 48             HttpContext.Current.Session.Timeout = 20;
 49         }
 50 
 51         /// <summary>
 52         /// 添加Session
 53         /// </summary>
 54         /// <param name="strSessionName">Session对象名称</param>
 55         /// <param name="strValue">Session值</param>
 56         /// <param name="iExpires">调动有效期(分钟)</param>
 57         public static void Add(string strSessionName, string strValue, int iExpires)
 58         {
 59             HttpContext.Current.Session[strSessionName] = strValue;
 60             HttpContext.Current.Session.Timeout = iExpires;
 61         }
 62 
 63         /// <summary>
 64         /// 添加Session
 65         /// </summary>
 66         /// <param name="strSessionName">Session对象名称</param>
 67         /// <param name="strValues">Session值数组</param>
 68         /// <param name="iExpires">调动有效期(分钟)</param>
 69         public static void Adds(string strSessionName, string[] strValues, int iExpires)
 70         {
 71             HttpContext.Current.Session[strSessionName] = strValues;
 72             HttpContext.Current.Session.Timeout = iExpires;
 73         }
 74 
 75         /// <summary>
 76         /// 读取某个Session对象值
 77         /// </summary>
 78         /// <param name="strSessionName">Session对象名称</param>
 79         /// <returns>Session对象值</returns>
 80         public static string Get(string strSessionName)
 81         {
 82             if (HttpContext.Current.Session[strSessionName] == null)
 83             {
 84                 return null;
 85             }
 86             else
 87             {
 88                 return HttpContext.Current.Session[strSessionName].ToString();
 89             }
 90         }
 91 
 92         /// <summary>
 93         /// 读取某个Session对象值数组
 94         /// </summary>
 95         /// <param name="strSessionName">Session对象名称</param>
 96         /// <returns>Session对象值数组</returns>
 97         public static string[] Gets(string strSessionName)
 98         {
 99             if (HttpContext.Current.Session[strSessionName] == null)
100             {
101                 return null;
102             }
103             else
104             {
105                 return (string[])HttpContext.Current.Session[strSessionName];
106             }
107         }
108 
109         /// <summary>
110         /// 删除某个Session对象
111         /// </summary>
112         /// <param name="strSessionName">Session对象名称</param>
113         public static void Del(string strSessionName)
114         {
115             HttpContext.Current.Session[strSessionName] = null;
116         }
117     }

 

posted @ 2016-09-23 08:53  百年明日能几何  阅读(231)  评论(0编辑  收藏  举报