http://www.cnblogs.com/heekui/archive/2006/08/15/477248.html
基本思路:
1 Session源网站设置Session数据同时,把SessionID和Session数据一起插入一个数据库中,再把SessionID作为查询字符串传递到Session获取网站.
2 Session获取网站从数据库中按SessionID查询获取Session数据并赋值到本网站的Session中.
示例:
Session源网站部分:
private void Button1_Click(object sender, System.EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.TextBox1.Text = Session.SessionID;
Session["Name"] = this.TextBox2.Text;
Session["Role"] = this.TextBox3.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
OleDbConnection conn = new OleDbConnection( @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\webTest.mdb;Persist Security Info=False" );
string strInsertSql = "insert into SessionData "
+ " ( SessionID, SessionName, SessionRole ) "
+ " values "
+ "( '" + Session.SessionID + "', '" + Session["Name"] + "', '" + Session["Role"] + "' )";
conn.Open();
OleDbCommand cmd = new OleDbCommand( strInsertSql, conn );
cmd.ExecuteNonQuery();
conn.Close();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.TextBox1.Text = "Session保存成功";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string strJumpUrl = "http://localhost/SessionReadFromOtherSite/ReadOtherSession.aspx?SessionId=" + Session.SessionID;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Response.Write("<script>window.open('" + strJumpUrl + "');</script>");
}
catch( System.Exception ex )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.TextBox1.Text = ex.Message;
}
}
Session获取网站部分:
private void Page_Load(object sender, System.EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ( Request.QueryString["SessionID"] != null )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
OleDbConnection conn = new OleDbConnection( @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\webTest.mdb;Persist Security Info=False" );
string strSql = "select "
+ " SessionID, SessionName, SessionRole "
+ " from SessionData "
+ " where SessionID = '" + Request.QueryString["SessionID"].ToString() + "'";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
OleDbDataAdapter da = new OleDbDataAdapter( strSql, conn );
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DataSet ds = new DataSet();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
da.Fill( ds );
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Session["Name"] = ds.Tables[0].Rows[0]["SessionName"].ToString();
Session["Role"] = ds.Tables[0].Rows[0]["SessionRole"].ToString();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.TextBox1.Text = ds.Tables[0].Rows[0]["SessionID"].ToString();
this.TextBox2.Text = Session["Name"].ToString();
this.TextBox3.Text = Session["Role"].ToString();
}
}
catch( System.Exception ex )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.TextBox1.Text = ex.Message;
}
}