⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sessiontransfer.aspx.cs

📁 asp.net和asp如何通用一个Session
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace pcf_solution_cs
{
	/// <summary>
	/// Summary description for SessionTransfer.
	/// </summary>
	public class SessionTransfer : System.Web.UI.Page
	{
		private void Page_Load(object sender, System.EventArgs e)
		{
			string guidSave;
			if (Request.QueryString["dir"] == "2asp")
			{
				//Add the session information to the database, and redirect to the
				//  ASP implementation of SessionTransfer.
				guidSave = AddSessionToDatabase();
				Response.Redirect("SessionTransfer.asp?dir=2asp&guid=" + guidSave + 
					"&url=" + Server.UrlEncode(Request.QueryString["url"]));
			}
			else
			{
				//Retreive the session information, and redirect to the URL specified
				//  by the querystring.
				GetSessionFromDatabase(Request.QueryString["guid"]);
				ClearSessionFromDatabase(Request.QueryString["guid"]);
				Response.Redirect(Request.QueryString["url"]);
			}
		}


		//This method adds the session information to the database and returns the GUID
		//  used to identify the data.
		private string AddSessionToDatabase()
		{

			//**************************************
			//Enter connection information here

			SqlConnection con = new SqlConnection("");
			
			//**************************************

			SqlCommand cmd = new SqlCommand();
			con.Open();
			cmd.Connection = con;
			int i = 0;
			string strSql, guidTemp = GetGuid();
			
			while (i < Session.Contents.Count)
			{
				strSql = "INSERT INTO ASPSessionState (GUID, SessionKey, SessionValue) " + 
					"VALUES ('" + guidTemp + "', '" + Session.Contents.Keys[i].ToString() + "', '" + 
					Session.Contents[i].ToString() + "')";
				cmd.CommandText = strSql;
				cmd.ExecuteNonQuery();
				i++;
			}

			con.Close();
			cmd.Dispose();
			con.Dispose();

			return guidTemp;
		}


		//This method retrieves the session information identified by the parameter
		//  guidIn from the database.
		private void GetSessionFromDatabase(string guidIn)
		{
			//**************************************
			//Enter connection information here

			SqlConnection con = new SqlConnection("");
			
			//**************************************
			SqlCommand cmd = new SqlCommand();
			SqlDataReader dr;
			con.Open();
			cmd.Connection = con;

			string strSql, guidTemp = GetGuid();
	
			//Get a DataReader that contains all the Session information
			strSql = "SELECT * FROM ASPSessionState WHERE GUID = '" + guidIn + "'";
			cmd.CommandText = strSql;
			dr = cmd.ExecuteReader();

			//Iterate through the results and store them in the session object
			while (dr.Read())
			{
				Session[dr["SessionKey"].ToString()] = dr["SessionValue"].ToString();
			}

			//Clean up database objects
			dr.Close();
			con.Close();
			cmd.Dispose();
			con.Dispose();
		}


		//This method removes all session information from the database identified by the 
		//  the GUID passed in through the parameter guidIn.
		private void ClearSessionFromDatabase(string guidIn)
		{
			//**************************************
			//Enter connection information here

			SqlConnection con = new SqlConnection("");
			
			//**************************************
			SqlCommand cmd = new SqlCommand();
			con.Open();
			cmd.Connection = con;
			string strSql;
			
			strSql = "DELETE FROM ASPSessionState WHERE GUID = '" + guidIn + "'";
			cmd.CommandText = strSql;
			cmd.ExecuteNonQuery();

			con.Close();
			cmd.Dispose();
			con.Dispose();
		}


		//This method returns a new GUID as a string.
		private string GetGuid()
		{
			return System.Guid.NewGuid().ToString();
		}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -