我讀了許多與配置管理器相關的主題,但無法解決問題。我只想讀取連接字符串以及Web應用程序中CLASS LIBRARY的一些appsetting鍵。c#中的ConfigurationManager有什麼問題嗎?
我引用了System.Configuration類。
這是我的代碼
using System.Configuration;
...
string constr = ConfigurationManager.ConnectionStrings["cbuddydb"].ConnectionString;
string strUserName = ConfigurationManager.AppSettings["username"];
string strPwd = ConfigurationManager.AppSettings["password"];
但似乎從一個不同的配置文件讀取。而不是從我的項目中的web.config。因爲讀出的值是錯誤的
請正確的代碼 編輯幫助:web.config中以下
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.data>
<connectionStrings>
<clear />
<add name="cbuddydb" connectionstring=
"Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=myDataBase;Persist Security Info=True;
[email protected];[email protected];Option=3" providerName="MySql.Data.MySqlClient" password=""/>
</connectionStrings>
<appSettings >
<clear />
<add key="username" value ="6/0RUNnSmUBsbdNoCg+9Sw=="/>
<add key="password" value =""/>
</appSettings>
</system.data>
</configuration>
也許你對調試/發佈有一些轉換? – wudzik
請發佈您的web.config,以及您的實際和預期結果。 –
在.NET配置系統中,** by design **,'ConfigurationManager'總是從**託管應用程序的**配置文件中讀取 - **從類庫自己的單獨文件中讀取**。如果您需要爲您的類庫配置設置 - 您必須將它們放到使用您的類庫的應用程序的'app.config'或'web.config'中 –