2012-09-17 30 views
1

嗨,我試圖首次使用和理解角色。 我該如何解決這個問題?我需要向WebConfig添加什麼嗎?我無法使用角色而不失敗。 「發生了網絡相關或特定於實例的錯誤」

private static void CreateRoleIfNotExists(string role) 
{ 
    if (!Roles.RoleExists(role)) // this line throws the error. 
    { 
     Roles.CreateRole(role); 
    } 
} 

更新,完整的堆棧跟蹤下面插入:

[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)] System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4856727 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +194 System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, SqlConnection owningObject) +4867325 System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject) +90 System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart) +374 System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +225 System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) +189 System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) +4868451 System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) +31 System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) +431 System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) +66 System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) +499 System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +65 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +117 System.Data.SqlClient.SqlConnection.Open() +122 System.Web.DataAccess.SqlConnectionHolder.Open(HttpContext context, Boolean revertImpersonate) +87 System.Web.DataAccess.SqlConnectionHelper.GetConnection(String connectionString, Boolean revertImpersonation) +221 System.Web.Security.SqlRoleProvider.RoleExists(String roleName) +478 System.Web.Security.Roles.RoleExists(String roleName) +73 dk.certifikat.Global.CreateRoleIfNotExists(String role)

+0

您的數據庫已關閉,或被防火牆阻止。 – McGarnagle

+0

@dbaseman在我登錄之前,我沒有使用任何數據庫。我錯過了一個數據源(對於我沒有使用的數據庫)? – radbyx

+0

檢查您的Web.Config,成員資格提供程序數據源的配置 – Massanu

回答

2

如果使用標準的.NET會員提供商和roleproviders你需要設置ASPNETDB

這可以通過完成命令從命令提示

aspnet_regsq l

這個可以在你的.net framework目錄下找到。 如果你這樣做。您需要爲應用程序提供一個連接到運行aspnetdb的數據庫的連接字符串。

更多關於此可以被發現here

+0

謝謝,我想我明白你的意思了。我想知道我是否走錯了道路,做錯了一切。我會重新考慮一下。 – radbyx

1

我也遇到過這個錯誤。我正在關注http://www.asp.net/web-forms/tutorials/aspnet-45/getting-started-with-aspnet-45-web-forms/introduction-and-overview的示例,他們在某個時刻也使用Roles.RoleExists。當我使用Visual Studio Express在本地運行應用程序時,一切正常。但是當我試圖通過網絡訪問應用程序時,我得到了相同的連接錯誤。我們不得不改變web.config文件。 我改變了DefaultProfileProvider,DefaultMembershipProvider和DefaultRoleProvider。我改變了的connectionStringName屬性到一個我已經加入的ConnectionStrings,即:

<add name="WingtipToys" providerName="System.Data.SqlClient" connectionString="Server=.\SQLEXPRESS;Database=wingtiptoys;Integrated Security=False;User Id=wingtiptoys;Password=wingtiptoys;AttachDbFileName=|DataDirectory|wingtiptoys.mdf;Persist Security Info=False" /> 

例如,DefaultMembershipProvider現在看起來像:

<add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="WingtipToys" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" /> 

的connectionStringName = 「WingtipToys」代替connectionStringName =「DefaultConnection」

然後一切正常。這可能不是最好的解決方案,但我希望這個答案可以幫助某人。

相關問題