Вы находитесь на странице: 1из 4

Using SQL Server to maintain session state

Article pulled from support.microsoft.com


Configure SQL Server for ASP.NET SQL Server Session State
The following steps describe how to run the InstallSqlState.sql and the UninstallSqlState.sql script files to
configure SQL Server mode session state management.
1.

In SQL Query Analyzer, on the File menu, click Open.

2.

In the Open Query File dialog box, browse to the InstallSqlState.sql script file, and then click Open. By
default, InstallSqlState.sql is located in one of the following folders:

system drive\WINNT\Microsoft.NET\Framework\version\

system drive\Windows\Microsoft.NET\Framework\version\

3.

After InstallSqlState.sql opens in SQL Query Analyzer, click Execute on the Query menu to run the script.

4.

Before you run the UninstallSqlState.sql script file to uninstall SQL Server mode session state management
configuration, you must stop the w3svc process. To do this, follow these steps:

a.

On the Windows Start menu, click Run, type cmd, and then click OK to open a command prompt.

b.

At the command prompt, type net stop w3svc. You receive confirmation that the w3svc process is
stopped.

2.

In SQL Query Analyzer, on the File menu, click Open.

3.

In the Open Query File dialog box, browse to the UninstallSqlState.sql script file, and then click Open. By
default, UninstallSqlState.sql is located in one of the following folders:

system drive\WINNT\Microsoft.NET\Framework\version\

system drive\Windows\Microsoft.NET\Framework\version\

4.

After UninstallSqlState.sql opens in SQL Query Analyzer, click Execute on the Query menu to run the
script.

5.

After you uninstall SQL Server mode session state management configuration, you must restart the w3svc
service. To restart the w3svc process, type net start w3svc at a command prompt.

Back to the top

Modify the Web.config File of Your Application


To implement ASP.NET SQL Server mode session state management, you must modify the <sessionState>
element of your application's Web.config file as follows:

1.

Set the mode attribute of the <sessionState> element to SQLServer to indicate that session state is
stored in SQL Server.

2.

Set the sqlConnectionString attribute to specify the connection string for SQL Server. For example:

3.

sqlConnectionString="data source=MySQLServer;user
id=<username>;password=<strongpassword>"

Note The user, <user name>, must have permissions to perform this operation on the database.

The modified <sessionState> element should appear as follows:

<sessionState
mode="SQLServer"
sqlConnectionString="data source=127.0.0.1;user
id=<username>;password=<strongpassword>"
cookieless="false"
timeout="20"
/>
USING the StateServer database for sessions:

C#: Session["variable_name"] = value;


VB.NET: Session("variable_name") = value
Once this value is stored, it's available throughout the user's session. The variable is
discarded when the session ends. You may circumvent the discarding of the value by
utilising persistent state management (which is a topic for another day).
ASP.NET state management
ASP.NET allows you to store session data in memory, via a state server, or in SQL
Server. The determination of the storage location is the application's Web.config file.
The sessionState element within the system.web element is where the state
management option is configured. The following example shows SQL Server utilised:
< sessionState
mode="SQLServer"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;user id=username;password=password"
cookieless="false"
timeout="20" />
Remember that the element names and attributes are case-sensitive. The following are
possible values for the mode attribute:

InProc--store in memory. This is the fastest for performance, but all data is lost
when the ASP.NET process recycles.

SQLServer--store data on SQL Server. This is the most reliable since it's
disconnected from the Web server. This option uses the sqlConnectionString option.
The connection string follows the normal syntax for connecting to a SQL Server
database.

StateServer--store data on a separate Web server (IIS). This option uses the
stateConnectionString attribute.
All options use the remaining. The cookieless attributes signal whether cookies are
stored in memory (false) or maintained in the QueryString/URL (true). The timeout
attribute signals the length of time (without activity) that session variables are stored.
Now let's turn our attention to SQL Server setup.
SQL Server setup
SQL Server requires a special database to handle state management. Thankfully, the
.NET Framework installation includes the necessary files to get this up and running in
no time. The following scripts are installed:

InstallPersistSqlState.sql--contains scripts to set up database for persistent


state management

InstallSqlState.sql--Contains scripts to set up database for state management

UninstallPersistSqlState.sql--Contains scripts for uninstalling persistent state


management

UninstallSqlState.sql--Contains scripts for uninstalling state management


These scripts may be run from Query Analyzer or via the isql.exe command-line utility.
To set up state management, we run InstallSqlState.sql. The result of the script is the
creation of a database named ASPState. This handles the storage and maintaining of
session variables. You can easily test the functionality with a simple example.
The following C# sample includes one Web form that populates session variables and
redirects to another Web form that displays the values:
< %@ Page language="c#" %>
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML><HEAD>
< title>WebForm1</title>
< meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
< meta name="CODE_LANGUAGE" Content="C#">
< /HEAD>
< body MS_POSITIONING="GridLayout">
< script language="C#" runat="server">
private void Page_Load(object sender, System.EventArgs e) {
Session["FirstName"] = "Tony";
Session["LastName"] = "Patton";
Session["Site"] = "Builder.com";
Response.Redirect("WebForm2.aspx", true);
}
< /script></body></HTML>
Here's the second Web form:

< %@ Page language="c#" %>


< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML><HEAD><title>WebForm2</title></HEAD>
< body>
< script language="C#" runat="server">
private readonly string newLine = "<br>";
private void Page_Load(object sender, System.EventArgs e) {
Response.Write(Session["FirstName"].ToString() + " ");
Response.Write(Session["LastName"].ToString() + newLine);
Response.Write(Session["Site"].ToString() + newLine);
}
< /script></body></HTML>
If you're a VB.NET developer, the pages have the following format:
< %@ Page Language="vb" %>
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
< html><head>
< title>WebForm1</title></head><body>
< script language="vb" runat="server">
Private Sub Page_Load(sender As Object, e As System.EventArgs)
Session("FirstName") = "Tony"
Session("LastName") = "Patton"
Session("Site") = "Builder.com"
Response.Redirect("WebForm2.aspx", true)
End Sub
< /script></body></html>
Here's the Page_Load event on the second form:
< %@ Page Language="vb" %>
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
< html><head>
< title>WebForm2</title></head><body>
< script language="vb" runat="server">
Private ReadOnly newLine As String = "<br>"
Private Sub Page_Load(sender As Object, e As System.EventArgs)
Response.Write(Session("FirstName").ToString() + " ")
Response.Write(Session("LastName").ToString() + newLine)
Response.Write(Session("Site").ToString() + newLine)
End Sub
< /script></body></html>
One note on uninstalling the state management feature: Microsoft recommends
stopping the World Wide Web Publishing service before executing the uninstall script.
You can accomplish this with the net stop w3svc command from a command line. You
can restart it with net start w3svc.
You can easily see the session management feature in action by examining the tempdb
database on the SQL Server. It will contain two temporary tables used for session
management: ASPStateTempApplications and ASPStateTempSessions.

Вам также может понравиться