Articles → .NET → Storing Session In Database
Storing Session In Database
- Memory of the web server (in process).
- Memory of the machine dedicated to store session variables (state server).
- In SQL Server database.
Advantages Of Storing Session In Database
- Scalability → Scalability is the web server's ability to maintain site availability, reliability and performance as the amount of web traffic increases. As the session state is stored in the database so the web server can maintain the availability, reliability and performance easily and hence the scalability increases.
- Reliability → As the data is physically persisted in the database, it is more reliable than any other option.
- Security → SQL server is safer as compared to the in-memory or the server state option.
Configuring SQL Server For Storing Session State
- Temporary storage → In this case session state is stored in the tempdb database. The tool creates the database ASPState and adds certain stored procedures for maintaining session into it. The tool also creates required tables in tempdb database. If you restart the SQL server the session state is not persisted.
- Persistent storage → In this case also the tool creates the ASPState database and create stored procedure for maintaining the state. But session state is stored in ASPState database. Data is persisted if you restart your SQL server.
- Custom storage → Both the session state data and stored procedures are stored in custom database. The database name is specified in configuration file.
Syntax
aspnet_regsql -ssadd(or -ssremove) -S <server_name> -E -sstype p
- -S → specifies the ip address or name of the sql server in which you want to store session state.
- -U → Specifies the userID to be used when connected to the web server.
- -P(Password) → Specifies the Password to be used when connected to the web server.
- -ssadd → Adds supports for the sql server mode session state.
- -ssremove → Removes supports for the sql server mode session state.
- -E → Indicates that you want to use integrated security while connecting to sql server.
- -sstype → Type of session state support. Options are
- t for temporary storage.
- p for persistent storage.
- c for custom storage.
- -d → Name of the custom database if sstype is c
aspnet_regsql -ssadd -S .sqlexpress -E -sstype p
Click to Enlarge
Configuring Website To Store The Session State
- Mode →
- Off → Indicates that session state is turned off.
- InProc → Indicates that session state is stored in the sever's memory.
- StateServer → Indicates that session state is stored in state server.
- SQLServer → Indicates that session state is stored in SQL server database.
- Custom → Indicates the custom mechanism for session state storage using provider model is asp.net.
- sqlConnectionString → If mode is SQLServer then you must specify this attribute. This attribute specifies the connection string of sql server database used for storing session state. If you are using custom storage options then only you need to specify the database name in the connection string.
- allowCustomSqlDatabase → If you want to store session state in a SQL Server database of your own, then you must set this attribute to true.
<sessionState mode="SQLServer" sqlConnectionString="data source=.sqlexpress; integrated security=true"> </sessionState>