Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home SQL Server ASP.NET Store Session In MS SQL Server Database

ASP.NET Store Session In MS SQL Server Database

When websites and applications execute in a clustered environment or require heavy state management functionality, you should use database to store session state information.

A single session variable is used to uniquely identify and associate the web visitor with the state full information held in the database.

This article describes How to configure SQL Server to store ASP.NET session state information.

Configure SQL Server for ASP.NET Session State Management

  • 1. Open & Execute the InstallSqlState.sql script file in the SQL Query Analyzer
    Open-Execute the InstallSqlState in SQL Query Analyzer

    Open-Execute the InstallSqlState in SQL Query Analyzer

    Note: normally, the InstallSqlState.sql script file is located in system_drive\Windows\Microsoft.NET\Framework_version\version\. Ex: C:\Windows\Microsoft.NET\Framework64\v4.0.30319

  • 2. In the application’s Web.config file, set mode=SQLServer and set the sqlConnectionString attribute in the sessionState element.

    + Example of the web.config file:

    <?xml version="1.0"?>
     
    <configuration>
     
        <system.web>
            <compilation debug="true" targetFramework="4.0" />
            <customErrors mode="Off"/>
     
            <sessionState
                mode="SQLServer"
                sqlConnectionString="data source=127.0.0.1;user id=sa;password=123456"
                cookieless="false"
                timeout="20"
            />
        </system.web>    
     
    </configuration>

    <?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> <customErrors mode="Off"/> <sessionState mode="SQLServer" sqlConnectionString="data source=127.0.0.1;user id=sa;password=123456" cookieless="false" timeout="20" /> </system.web> </configuration>

    Note: please ensure the database user name has permissions to perform this operation on the database.

  • 3. Create a simple asp.net (C#) page for testing:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
     
    public partial class test_session_stores_in_mssql_database : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["session_variable"] = "123456";
     
            Response.Write("session_variable: " + Session["session_variable"].ToString());
        }
    }

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class test_session_stores_in_mssql_database : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Session["session_variable"] = "123456"; Response.Write("session_variable: " + Session["session_variable"].ToString()); } }

  • 4. View it on a browser
  • 5. Verify if the ASP.NET session state is stored somewhere in SQL Server database. Try to use SQL Server Profiler to check if any stored procedures or SQL statements are called when you read/write ASP.NET session or try to run these SQL statements:
    SELECT * FROM [tempdb].dbo.ASPStateTempApplications
    SELECT * FROM [tempdb].dbo.ASPStateTempSessions

    SELECT * FROM [tempdb].dbo.ASPStateTempApplications SELECT * FROM [tempdb].dbo.ASPStateTempSessions

    Query ASPStateTempApplications and ASPStateTempSessions Tables

    Query ASPStateTempApplications and ASPStateTempSessions Tables

Feb 25, 2012Hoan Huynh
Javascript LTrim,RTrim,Trim functionPHP Store Session In MySQL Database
You Might Also Like:
  • PHP Store Session In MySQL Database
  • Where Are ASPStateTempApplications and ASPStateTempSessions Tables?
  • Export/Import Data From Local Database To Remote Database With MS SQL Server 2008
  • How To Filter DataBase Name And Login Username In SQL Server Profiler
  • MSSQL Create Database With SQL Statement
  • Schedule Backup And Zip MySQL Database In Windows
  • Capture And Display Execution Time Of SQL Query In SQL Server
  • Consider SQL Server Express 2008 Limitations
  • Fix session_start() open(/tmp) failed Permission Denied With .htaccess, ini_set and php.ini
  • MS SQL How to get all database names
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

10 years ago SQL ServerInstallSqlState, sessionState, SQL Query Analyzer, SQL Server Profiler, SQLServer589
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,558 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,892 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,746 views
JQuery Allow only numeric characters or only alphabet characters in textbox
15,071 views
C# Read Json From URL And Parse/Deserialize Json
11,805 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development