J'ai un XP 32 bits tournant VS 2008 et j'essaie de décrypter ma chaîne de connexion de mon fichier web.config dans mon fichier C# ASPX.Comment accéder à la chaîne de connexion web.config en C#?
Même s'il n'y a pas d'erreur renvoyée, ma chaîne de connexion actuelle n'affiche pas le contenu de ma procédure stockée AdventureWorks sélectionnée.
J'y suis entré:
C:\Program Files\Microsoft Visual Studio 9.0\VC>Aspnet_regiis.exe -pe "connectionStrings" -app "/AddFileToSQL2"
Ensuite, il a dit "succeeded".
Et ma section web.config ressemble:
<connectionStrings>
<add name="Master" connectionString="server=MSSQLSERVER;database=Master; Integrated Security=SSPI"
providerName="System.Data.SqlClient" />
<add name="AdventureWorksConnectionString" connectionString="Data Source=SIDEKICK;Initial Catalog=AdventureWorks;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="AdventureWorksConnectionString2" connectionString="Data Source=SIDEKICK;Initial Catalog=AdventureWorks;Persist Security Info=true; "
providerName="System.Data.SqlClient" />
</connectionStrings>
Et mon code C# regarde derrière comme:
string connString = ConfigurationManager.ConnectionStrings["AdventureWorksConnectionString2"].ConnectionString;
Y at-il quelque chose de mal avec la chaîne de connexion dans le web.config ou C# code derrière le fichier?
Je mis un point d'arrêt dans le code C# derrière et maintenant je l'exception ci-dessous:
System.Data.SqlClient.SqlException was caught
Message="Login failed for user ''."
Source=".Net SqlClient Data Provider"
ErrorCode=-2146232060
Class=14
LineNumber=65536
Number=18456
Procedure=""
Server="SIDEKICK"
State=1
StackTrace:
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Open()
at ADONET_namespace.ADONET_methods.DisplaySchemaTables() in C:\Documents and Settings\Admin\My Documents\Visual Studio 2008\Projects\AddFileToSQL2\AddFileToSQL\Admins\ADONET methods.cs:line 65
InnerException:
De plus, j'ai ajouté un contrôle Web LoginView pour sécuriser mon site. Le nom de connexion est "testeur".
Pourquoi pensez-vous qu'il y a un problème avec la chaîne de connexion? Avez-vous vérifié le sproc? Est-ce qu'il renvoie des données lorsque vous l'appelez directement à partir de SQL Management Studio ou lorsque vous passez le code? –
Avez-vous une erreur? Au lieu de "Persist Security Info = true" essayez ceci dans Connectionstring2 "Integrated Security = True" – Raja
Oui, le sproc renvoie correctement les données dans SSMS. Cependant, j'ai mis un point d'arrêt à cette partie dans le code C# derrière et je mets à jour cette exception ci-dessus. – salvationishere