关闭

写了个缓存 Session 的类

标签: sessionapplicationstringclassnulltable
695人阅读 评论(0) 收藏 举报
分类:

 

    public class SessionCache
    
{
        
Get New Cache Table

        
public static void SessionStart(DataTable table, string strSessionId, string strHostAddress, string strPlatForm, string strBrowserType, DateTime dtNow)
        
{
            DataRow row 
= table.NewRow();

            DateTime now 
= DateTime.Now;

            row[
"SessionId"= strSessionId;
            row[
"HostAddress"= strHostAddress;
            row[
"PlatForm"= strPlatForm;
            row[
"BrowserType"= strBrowserType;
            row[
"StartTime"= dtNow;

            table.Rows.Add(row);
        }


        
public static void SessionEnd(DataTable table, string strSessionId, DateTime dtNow)
        
{
            DataRow[] rows 
= table.Select(string.Format("SessionId = '{0}'", strSessionId));

            
if( rows.Length == 0 )
                
return;

            rows[
0]["EndTime"= dtNow;

            
string connString = ConfigurationSettings.AppSettings["ConnString"];
            
using( SqlConnection conn = new SqlConnection(connString) )
            
{
                SqlCommand cmd 
= new SqlCommand();
                cmd.Connection 
= conn;

                
if( rows[0]["UserCode"!= DBNull.Value )
                
{
                    cmd.CommandText 
= "INSERT INTO SessionLog(SessionId, UserCode, HostAddress, PlatForm, BrowserType, StartTime, LoginTime, EndTime) VALUES(@SessionId, @UserCode, @HostAddress, @PlatForm, @BrowserType, @StartTime, @LoginTime, @EndTime)";
                    cmd.Parameters.Add(
"@UserCode", Convert.ToInt32(rows[0]["UserCode"].ToString()));
                    cmd.Parameters.Add(
"@LoginTime", Convert.ToDateTime(rows[0]["LoginTime"].ToString()));
                }

                
else
                
{
                    cmd.CommandText 
= "INSERT INTO SessionLog(SessionId, HostAddress, PlatForm, BrowserType, StartTime, EndTime) VALUES(@SessionId, @HostAddress, @PlatForm, @BrowserType, @StartTime, @EndTime)";
                }


                cmd.Parameters.Add(
"@SessionId", rows[0]["SessionId"].ToString());
                cmd.Parameters.Add(
"@HostAddress", rows[0]["HostAddress"].ToString());
                cmd.Parameters.Add(
"@PlatForm", rows[0]["PlatForm"].ToString());
                cmd.Parameters.Add(
"@BrowserType", rows[0]["BrowserType"].ToString());
                cmd.Parameters.Add(
"@StartTime", Convert.ToDateTime(rows[0]["StartTime"].ToString()));
                cmd.Parameters.Add(
"@EndTime", Convert.ToDateTime(rows[0]["EndTime"].ToString()));

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }


            table.Rows.Remove(rows[
0]);
        }


        
public static void UserLogin(DataTable table,string strSessionId, int intUserCode, string strUserName, DateTime dtNow)
        
{
            DataRow[] rows 
= table.Select(string.Format("SessionId = '{0}'", strSessionId));

            
if( rows.Length == 0 )
                
return;

            rows[
0]["UserCode"= intUserCode;
            rows[
0]["LoginName"= strUserName;
            rows[
0]["LoginTime"= dtNow;
        }

    }


    public class Global : System.Web.HttpApplication
    
{
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.IContainer components = null;

        
public static string ErrorMessage = "NULL";

        
public Global()
        
{
            InitializeComponent();
        }
    
        
        
protected void Application_Start(Object sender, EventArgs e)
        
{
            
try
            
{
                Application[
"SessionCount"= 0;
                Application[
"SessionCache"= SessionCache.CreateCacheTable();
            }

            
catch(Exception ex)
            
{
                Global.ErrorMessage 
= ex.Message + "<br>" + ex.Source + "<br>" + ex.StackTrace;
            }

        }

 
        
protected void Session_Start(Object sender, EventArgs e)
        
{
            
try
            
{
                Application.Lock();
                Application[
"SessionCount"= (int)Application["SessionCount"+ 1;
                SessionCache.SessionStart(
                    (System.Data.DataTable)Application[
"SessionCache"], 
                    Session.SessionID, 
                    Request.UserHostAddress, 
                    Request.Browser.Platform, 
                    Request.Browser.Type, 
                    DateTime.Now
                    );
                Application.UnLock();
            }

            
catch(Exception ex)
            
{
                Global.ErrorMessage 
= ex.Message + "<br>" + ex.Source + "<br>" + ex.StackTrace;
            }

        }


        
protected void Application_BeginRequest(Object sender, EventArgs e)
        
{

        }


        
protected void Application_EndRequest(Object sender, EventArgs e)
        
{

        }


        
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
        
{

        }


        
protected void Application_Error(Object sender, EventArgs e)
        
{

        }


        
protected void Session_End(Object sender, EventArgs e)
        
{
            
try
            
{
                Application.Lock();
                Application[
"SessionCount"= (int)Application["SessionCount"- 1;
                SessionCache.SessionEnd(
                    (System.Data.DataTable)Application[
"SessionCache"], 
                    Session.SessionID, 
                    DateTime.Now
                    );
                Application.UnLock();
            }

            
catch(Exception ex)
            
{
                Global.ErrorMessage 
= ex.Message + "<br>" + ex.Source + "<br>" + ex.StackTrace;
            }

        }


        
protected void Application_End(Object sender, EventArgs e)
        
{

        }

            
        
Web 窗体设计器生成的代码
    }

 

http://weboy.cnblogs.com/archive/2006/06/06/418668.html

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2261256次
    • 积分:25854
    • 等级:
    • 排名:第228名
    • 原创:1篇
    • 转载:1662篇
    • 译文:0篇
    • 评论:98条
    最新评论