我有一個32位XP運行VS 2008,我試圖從我的C#ASPX文件中的web.config文件解密我的連接字符串。如何訪問C#中的web.config連接字符串?
即使沒有返回錯誤,我當前的連接字符串也不會顯示我選擇的AdventureWorks存儲過程的內容。
我進入它:
C:\Program Files\Microsoft Visual Studio 9.0\VC>Aspnet_regiis.exe -pe "connectionStrings" -app "/AddFileToSQL2"
然後它說: 「成功」。
我的web.config部分,看起來像:
<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>
而我的C#代碼背後的樣子:
string connString = ConfigurationManager.ConnectionStrings["AdventureWorksConnectionString2"].ConnectionString;
有什麼不對的連接字符串在web.config或C#代碼在文件後面?
我在C#代碼中設置一個斷點後面,現在我得到下面的異常:
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:
另外,我增加了一個LoginView網絡控制,以確保我的網站。登錄名是「tester」。
爲什麼你認爲這是連接字符串的問題?你有沒有檢查sproc?當你從sql管理工作室直接調用它或者在單步執行代碼時它是否返回數據? – 2010-04-24 17:15:18
你有什麼錯誤嗎?而不是「Persist Security Info = true」,請在Connectionstring2「Integrated Security = True」中嘗試此操作 – Raja 2010-04-24 17:21:43
是的,在SSMS中,sproc正確返回數據。不過,我在後面的C#代碼的這一部分設置了一個斷點,並且正在更新上面的這個異常。 – salvationishere 2010-04-24 17:22:07