C#操作session的類實例(轉)


using System.Web;
namespace DotNet.Utilities
{
 public static class SessionHelper2
 {
  /// <summary>
  /// 添加Session,調動有效期為20分鍾
  /// </summary>
  /// <param name="strSessionName">Session對象名稱</param>
  /// <param name="strValue">Session值</param>
  public static void Add(string strSessionName, string strValue)
  {
   HttpContext.Current.Session[strSessionName] = strValue;
   HttpContext.Current.Session.Timeout = 20;
  }
  /// <summary>
  /// 添加Session,調動有效期為20分鍾
  /// </summary>
  /// <param name="strSessionName">Session對象名稱</param>
  /// <param name="strValues">Session值數組</param>
  public static void Adds(string strSessionName, string[] strValues)
  {
   HttpContext.Current.Session[strSessionName] = strValues;
   HttpContext.Current.Session.Timeout = 20;
  }
  /// <summary>
  /// 添加Session
  /// </summary>
  /// <param name="strSessionName">Session對象名稱</param>
  /// <param name="strValue">Session值</param>
  /// <param name="iExpires">調動有效期(分鍾)</param>
  public static void Add(string strSessionName, string strValue, int iExpires)
  {
   HttpContext.Current.Session[strSessionName] = strValue;
   HttpContext.Current.Session.Timeout = iExpires;
  }
  /// <summary>
  /// 添加Session
  /// </summary>
  /// <param name="strSessionName">Session對象名稱</param>
  /// <param name="strValues">Session值數組</param>
  /// <param name="iExpires">調動有效期(分鍾)</param>
  public static void Adds(string strSessionName, string[] strValues, int iExpires)
  {
   HttpContext.Current.Session[strSessionName] = strValues;
   HttpContext.Current.Session.Timeout = iExpires;
  }
  /// <summary>
  /// 讀取某個Session對象值
  /// </summary>
  /// <param name="strSessionName">Session對象名稱</param>
  /// <returns>Session對象值</returns>
  public static string Get(string strSessionName)
  {
   if (HttpContext.Current.Session[strSessionName] == null)
   {
    return null;
   }
   else
   {
    return HttpContext.Current.Session[strSessionName].ToString();
   }
  }
  /// <summary>
  /// 讀取某個Session對象值數組
  /// </summary>
  /// <param name="strSessionName">Session對象名稱</param>
  /// <returns>Session對象值數組</returns>
  public static string[] Gets(string strSessionName)
  {
   if (HttpContext.Current.Session[strSessionName] == null)
   {
    return null;
   }
   else
   {
    return (string[])HttpContext.Current.Session[strSessionName];
   }
  }
  /// <summary>
  /// 刪除某個Session對象
  /// </summary>
  /// <param name="strSessionName">Session對象名稱</param>
  public static void Del(string strSessionName)
  {
   HttpContext.Current.Session[strSessionName] = null;
  }
 }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM