关于c#:如何使用会话状态模式:带VS2010的SQL Server

How to use Session state mode : SQL Server with VS2010

我想使用该会话状态,因为inproc不可靠。我需要在配置文件中设置该模式,但不知道如何设置。有人能帮我吗?

从aspnet_regsql工具得到的异常

1
xception:

无法连接到SQL Server数据库。

故障详情

System.Web.httpException(0x80004005):无法连接到SQL Server数据库。--->system.data.sqlclient.sqlexception(0x80131904):与SQL Server建立连接时发生网络相关或特定于实例的错误。找不到或无法访问服务器。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。(提供程序:命名管道提供程序,错误:40-无法打开到SQL Server的连接)在system.data.sqlclient.sqlInternalConnection.onerror(sqlException异常,布尔breakConnection)在system.data.sqlclient.tdsarser.throwExceptionandWarning()处在system.data.sqlclient.tdsparser.connect上(serverinfo serverinfo、sqlinterconnectiontds connhandler、boolean ignoreniopentimeout、int64 timerexpire、boolean encrypt、boolean trustservercert、boolean integrated security)位于system.data.sqlclient.sqlInternalConnectionTds.attemptoneLogin(serverInfo serverInfo,string newpassword,boolean ignoreniopentimeout,timeouttimer timeout,sqlconnection owningobject)位于system.data.sqlclient.sqlInternalConnectionTds.loginNoFailover(serverInfo serverInfo,string newpassword,boolean redirectedUserInstance,sqlconnection owningObject,sqlConnectionString connectionOptions,timeoutTimer timeout)位于system.data.sqlclient.sqlInternalConnectionTds.openLogineList(sqlConnection OwningObject、TimeoutTimer Timeout、sqlConnectionString ConnectionOptions、String NewPassword、Boolean RedirectedUserInstance)位于system.data.sqlclient.sqlInternalConnectionTds..ctor(dbConnectionPoolIdentity、sqlConnectionString ConnectionOptions、Object ProviderInfo、String NewPassword、sqlConnection OwningObject、Boolean RedirectedUserInstance)位于system.data.sqlclient.sqlconnectionFactory.createConnection(dbConnectionOptions选项,对象PoolGroupProviderInfo,dbConnectionPool池,dbConnection OwningConnection)位于system.data.providerBase.dbConnectionFactory.createPooledConnection(dbConnection OwningConnection、dbConnectionPool池、dbConnectionOptions选项)位于System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection OwningObject)位于System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection OwningObject)在system.data.providerBase.dbConnectionPool.getConnection处(dbConnection OwningObject)在system.data.providerBase.dbConnectionFactory.getConnection处(dbConnection owningConnection)位于system.data.providerBase.dbConnectionClosed.openConnection(dbConnection outerConnection,dbConnectionFactory ConnectionFactory)在system.data.sqlclient.sqlconnection.open()处在system.web.management.sqlservices.getsqlconnection(string server、string user、string password、boolean trusted、string connectionstring)在system.web.management.sqlservices.getsqlconnection(string server、string user、string password、boolean trusted、string connectionstring)在system.web.management.sqlservices.setupapplicationservices(string server、string user、string password、boolean trusted、string connectionstring、string database、string dbfilename、sqlfeatures features、boolean install)在system.web.management.confirmpanel.execute()上


如果没有在SQL上创建会话数据库,则需要首先运行一个工具,使用aspnet_regsql.exe工具创建该数据库。

1
2
DATABASENAME = AnyNameOfServerState
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>aspnet_regsql.exe -ssadd -sstype c -d DATABASENAME -E

此工具的详细信息:http://msdn.microsoft.com/en-us/library/ms229862(vs.80).aspx

其次,您可以使用集成的安全性进行连接,而不使用用户名和密码。在连接字符串上设置此项

1
 

转到system.web

1
<sessionState mode="SQLServer" sqlConnectionString="SqlState" allowCustomSqlDatabase="true" cookieless="false" timeout="20"/>

更多细节。如果转到网络目录,您会看到扩展名为.sql的文件列表。其中之一是installsqlstate.sql,它是工具运行以创建数据库的工具。如果你打开它,你可以看到什么是运行。


还可以使用StateServer。您可以在这里遵循分步指南

http://dotnetguts.blogspot.com/2009/06/steps-for-session-inproc-mode-to.html

我希望您了解每个会话状态的优缺点,这样您就可以为您的应用程序选择正确的会话状态。