能否将ASP.NET会话状态存储到应用程序数据库而非ASPState数据库?
Great question—let’s break down your concerns one by one with practical, actionable details:
Can I use my application's own database to store session state?
Absolutely! You don’t need a separate database for session state. ASP.NET fully supports storing session data directly in your existing application database by running the required setup scripts against it.
To make this work:
Use the
aspnet_regsql.exetool (typically located inC:\Windows\Microsoft.NET\Framework\<your-dotnet-version>) with parameters targeting your existing database. Example command:aspnet_regsql.exe -S YourDatabaseServer -U YourUsername -P YourPassword -d YourAppDatabaseName -ssadd -sstype pThe
-sstype pflag tells the tool to create session state tables and stored procedures directly in your specified database (instead of relying on the default ASPState database with temp tables).Update your
web.configto point the session state connection string to your application database:<sessionState mode="SQLServer" sqlConnectionString="Data Source=YourDatabaseServer;Initial Catalog=YourAppDatabaseName;User ID=YourUsername;Password=YourPassword" timeout="20" />
Is there a hard-coded requirement for the database to be named ASPState?
No, there’s no hard-coded restriction on the database name. ASPState is just the default name the setup script uses when you don’t explicitly specify a target database. As shown above, the -d parameter in aspnet_regsql.exe lets you use any existing database (including your app’s own) to host session state objects.
Do I have to create a new database to implement this feature?
Not at all. The setup script can generate all necessary session state tables (like ASPStateTempSessions, ASPStateTempApplications) and stored procedures directly in your existing application database. This lets you avoid managing a separate database solely for session state, keeping all your application-related data consolidated in one place.
Just ensure the database user in your connection string has permissions to read/write to the session state tables and execute the associated stored procedures.
内容的提问来源于stack exchange,提问作者Grigoryants Artem

