Saving Data to an SQL Database
In This Topic
Database state management saves data to a specified database. Using an SQL database for state management is best if you are working with large amounts of data, particularly data that needs to be secure and maintain integrity.
To save data to an SQL database, you must install SQL State Management, as explained in the example.
Advantages and Disadvantages
The advantages of using a database to maintain state are:
- Databases are typically very secure, requiring rigorous authentication and authorization.
- Databases offer large capacity.
- Database information can be stored as long as you like, and it is not subject to the availability of the Web server.
- Databases include various facilities for maintaining data integrity.
- Data stored in your database is accessible to a wide variety of information-processing tools.
- There are numerous database tools available, offering wide support and custom configurations.
The disadvantages of using a database to maintain state are:
- Using a database to support state management requires more complex hardware and software configurations.
- Using a database can affect performance, for example due to poor construction of the relational data model. Also, large numbers of queries to the database can adversely affect server performance.
Using Code
This example describes how to install SQL state management. To install SQL State Management complete the following instructions.
- Do one of the following:
- If you are using the MSDE version of SQL Server that ships with Visual Studio.NET you need to open a command prompt window and navigate to the Windows\Microsoft.Net\Framework\(Version) directory. Once there, issue the following command to run the InstallSqlState.sql script:
OSQL –S localhost –U sa –P <InstallSqlState.sql
- OSQL.exe is a tool that ships with MSDE and SQL Server. It allows you to apply a T-SQL script to a SQL Server.
- If you are using SQL Server 7 or SQL Server 2000 you can follow the directions above or you can open the Enterprise Manager, open the InstallSqlState.sql script from the Windows\Microsoft.Net\(Framework Version) directory, and execute the script from there.
- Whichever method you choose, the script will set up an ASPState database in your SQL Server Group Databases.
- After you have run the script to set up SQL state management, you need to make a change to your project’s web.config file. Under the session State section, change the Mode setting from its current setting (most likely InProc) to SQL Server.
- Then configure the sqlConnectionString to point to the SQL Server where you installed the T-SQL script InstallSqlState.sql as follows:
sqlConnectionString="data source=127.0.0.1;user id=sa;password=;"
See Also