How to store Session in SQL database, sessionsql Database
In aspnet, sessions are stored in inproc mode by default, that is, they are stored in iis processes. This has the advantage of high efficiency, but is not conducive to the expansion of Server Load balancer. The session information can be stored in SQL Server. It is said that the performance loss of this method compared with inproc is 10%-20%. How to implement it is described in two steps:
1. initialize the status database in SQL Server
ASP. net SQL Server registration tool aspnet_regsql.exe is used to create a Microsoft SQL Server database for SQL Server providers in ASP. NET. Aspnet_regsql.exe is located in the/% windir %/Microsoft. NET/Framework/<versionNumber>/aspnet_regsql.exe directory. If it is troublesome, you can directly input aspnet_regsql.exe using visual studio tools's command prompt tool. The usage is as follows:
Aspnet_regsql.exe <options>
You can use the following syntax to add the default session database ASPState.
Aspnet_regsql.exe-S localhost-U sa-P why1234? -Ssadd-sstype p
-S,-U/-P
It must be in uppercase, indicating the database server, user name, and password respectively.
-Ssadd/-ssremove parameters:
-Ssadd indicates that the Session database is added, and-ssremove indicates that the Session database is removed.
Sstype parameters:
|
Option |
Description |
|
T |
Store session data in the SQL Server tempdb database. This is the default setting. If session data is stored in the tempdb database, session data is lost when SQL Server is restarted. |
|
P |
Store session data in the ASPState database instead of the tempdb database. |
|
C |
Store session data in a custom database. If you specifyCOption, you must also use-DThe options include the name of the custom database. |
You can use the following syntax to create a custom database myAppState:
Aspnet_regsql.exe-S localhost-U sa-P why1234? -Ssadd-sstype c-d myAppState
2. Configure webconfig
Add the following configuration in the <system. web> section of webconfig:
<SessionState mode = "SQLServer" sqlConnectionString = "server = localhost; uid = sa; pwd = 123456;"/>
If a user-defined database is created during database initialization, the following configurations can be used:
<SessionState mode = "SQLServer" allowCustomSqlDatabase = "true" sqlConnectionString = "server = localhost; DataBase = myAspState; uid = sa; pwd = 123456;"/>
You can use the settings in the preceding two steps. For details, see msdn.