How to store Session in SQL database, sessionsql Database

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.