[ASP. NET] configure to use SQL Server to save session Status

Source: Internet
Author: User

Session status is ASP. NET applicationProgramOne of the statuses, which is characterized by users. Each session corresponds to a user, that is, throughout the user's access process, data stored in the session state is stored in the media of the server until the session times out (that is, the user has not accessed the session data after the specified time ). There are several ways to save the session status on the server:

1) Inproc -- In this way, the session state is stored in the ASP. NET running process, and the specific location is the cache of ASP. NET (high-speed cache. 2) StateServer -- In this way, the session state is stored in the wiindows service process in ASP. NET. This service is disabled by default and needs to be started manually. 3) SQL Server -- In this way, session status is saved to the database managed by SQL Server. The data can be stored in temporary databases, aspstate databases, and user-defined databases. 4) Custom providers -- This method requires developers to develop their own session state providers. The access method of session state is completely determined by the provider, which can be stored in other SQL databases or XML files, it can even be used in text files. This article describes how to configure SQL Server to save session status. The configuration process is divided into two parts: the first part is to configure the database server used on the server where ASP. NET is running. The second part is to modify the session state saving mode in the configuration file of the ASP. NET program. Configure the database server used : 1. Run the aspnet_regsql.exe program. The program is located in the c: \ windows \ Microsoft. NET \ framework \ <asp. Net version number> directory. Double-click to run the program and configure SQL Server for ASP. NET. Select "Next" and select as shown in the figure, and then fill in "Next" with ASP. net associated with the database server information, pay attention to the Database Selected here, in the future ASP. NET applications can be changed. After entering the information, select "Next" and there are two interfaces. However, in those two interfaces, you only need to "Next" and there is no selectable information, so this is not listed here. 2) database used to configure the session status. As mentioned earlier, there are three ways to save sessions using databases: Temporary databases (data is lost after SQL Server is restarted), aspstate databases, and custom databases. Note the-ssadd,-E,-sstype commands. -Ssadd The function of the command is to add a session database, which is added to a temporary database by default. -E The command function is Windows authentication. Because the SQL server I demonstrated is on the local computer, this command can be used for connection, and the separate-ssadd command cannot run. -Sstype The command is used to specify the database used by the session status. Subsequent parameters include T, P, and C, which are temporary databases, aspstate database databases, and custom databases respectively. -D The function is to specify the name of a user-defined database and use the database name as a parameter. Command for saving session status using temporary database : Two more tables in the temporary database are used to save session status information.

Command for saving session status using the aspstate Database : After execution, a new database aspstate is displayed in the database. The included tables are consistent with those in the temporary database.

Command for saving session status using the custom database testcustom : In addition to the tables mentioned above, there are also a series of automatically added stored procedures. In fact, these stored procedures are not unique to custom databases. No matter which database you use, these stored procedures will exist, but when you change from one database to another, the tables will stay, however, all stored procedures will be transferred to the new database. To cancel the preceding settings, you only need the-ssremove-e command. These operations can only be used to create a session database in the local database, but the formal operation is not to directly perform these operations on the database server, it is likely that there is no ASP on the server.. net. Therefore, we need to configure a remote connection from the deployment server of the ASP. NET program. The following shows how to connect this instance to the remote computer of the team member through my computer. To remotely connect to aspnet_regsql.exe, run the-S-u-p command to specify the database server name/address and the account and password used for the connection. It must also be noted that to configure the session database in SQL Server, you must have Create tables, create databases, create stored procedures, and MSDB rsexecrole role Permissions . Otherwise, the configuration process will fail. The preceding configuration commands are run on the machine 119.120.74.150 and the account and password are all silin to establish a connection. Then, a custom database testcustom is created to save the session status. The above is the configuration of SQL server used by ASP. NET. Next, modify the configuration file. Modify the configuration file of the ASP. NET Program : Find the <sessionstate> section in the configuration file of ASP. NET. It is likely that this section does not exist because of the default settings. If this section does not exist, write it again. <Sessionstate mode = "sqlserver" sqlconnectionstring = "required connection string"/> you only need to set this simple setting. Of course there are other settings, but to make a difference, at least the two attribute settings are OK. After the preceding settings are complete, the session data accessed in the program is automatically saved to the database, and the entire encoding process is not changed. The ASP. NET provider mechanism does Code The implementation is separated from the data storage method. However, when saving the session status to ASP.. net will be serialized and deserialized when the media is running outside the process. Therefore, you must pay attention to whether the types saved in the session state can be serialized.
Related Article

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.