雷火电竞-中国电竞赛事及体育赛事平台

歡迎來(lái)到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁(yè) > 軟件編程 > C#教程 >

C#程序中session的基本設(shè)置示例及清除session的方法

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊:

session的基本設(shè)置:

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace OAFrameWork
{
  public class CSession
  {
      public static object Get(string Key)
    {
      return HttpContext.Current.Session[Key];
    }


  public static string GetString(string Key)
    {
      object obj = HttpContext.Current.Session[Key];
      if (obj == null) return "";
      else return obj.ToString();
    }


    public static object Get(string Key,object DefaultValue)
    {
      if (HttpContext.Current.Session[Key] == null)

        return DefaultValue;
      else
        return HttpContext.Current.Session[Key];

    }


    public static object Get(string Key, object DefaultValue,Boolean CanAdd)
    {
      if (HttpContext.Current.Session[Key] == null)
      {
        if(CanAdd==true)
          HttpContext.Current.Session.Add(Key, DefaultValue);

        return DefaultValue;
      }
      else
        return HttpContext.Current.Session[Key];

    }


    public static Boolean Set(string Key,object Value)
    {
      try
      {
        if (Value == null && HttpContext.Current.Session[Key] != null)
        {
          HttpContext.Current.Session.Remove(Key);
        }
        else if (HttpContext.Current.Session[Key] == null)
          {
            HttpContext.Current.Session.Add(Key, Value);
          }
          else
          {
            HttpContext.Current.Session[Key] = Value;
          }

        return true;
      }
      catch (Exception ex)
      {
        CMsgBox.Show(ex.Message); 
        return false;
      }

    }
 

      
    
  }
}

清除Session:

Session.Abandon();//清除全部Session
//清除某個(gè)Session
Session["UserName"] = null;
Session.Remove("UserName");

上一篇:C# 運(yùn)算符 ?、??、?: 各種問(wèn)號(hào)的用法和說(shuō)明

欄    目:C#教程

下一篇:C#中使用XmlDocument類來(lái)創(chuàng)建和修改XML格式的數(shù)據(jù)文件

本文標(biāo)題:C#程序中session的基本設(shè)置示例及清除session的方法

本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6592.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有