How to use the State management database in ASP. NET to store session variable.

Source: Internet
Author: User

(Domestic version)

Many people will think of using cookies (Httpcookies) To store certain data, so that the stored data can still be read from the user end when visiting another webform. However, if the user's browser does not allow cookies... Isn't that all done... Moreover, cookies can only contain 4 K bytes of data!

Dim Democookie As   New Httpcookie ( " Samplecookie " )

Dim Time As Datetime = Datetime. Now

Democookie. Values. Add ( " Time " , Time. tostring ())

Democookie. Expires = Time. adddays ( 5 )

Response. Cookies. Add (democookie)

Response. Redirect ( " Sample. aspx " )


Therefore, we need to use the SQL database of the server side instead of the cookies of the client side. What's more, it is very simple to use the SQL database, just install it. installsqlstate provided by net framework. SQL script and modify the web. config file. The others are exactly the same as the session variables normally written.

Installsqlstate. SQL can be found inC: \ windows \ Microsoft. NET \ framework \ (Version Number )\Folder.

The sessionstate attribute in Web. config to be modified is as follows:

< Sessionstate

Mode = "Sqlserver"

Stateconnectionstring = "TCPIP = FIG: 42424"

Sqlconnectionstring = "Data Source = localhost; Integrated Security = true"

Cookieless = "False"  

Timeout = "20"  

/>

Set the original

1. Change Mode = inproc to mode = sqlserver.

2. Modify the sqlconnectionstring, but you do not need to enter the initial catalog here. Because aspstate is the mo-person catalog,

This is also the ASP state management database installed by installsqlstate. SQL.

3. Change timeout to the expected session expire value. The unit of timeout is measured in minutes.

4. cookieless = "false" indicates that the sesionid is contained in the URL, for example, http: // server/(sessionid)/sample. aspx.

Therefore, the value of this attribute is generally false.

Dim Time As Datetime = Datetime. Now

Session ( " Time " ) = Time. tostring ())

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.