问题描述
我有一个使用的app.config来存储它的数据库连接字符串一个C#程序集。当调试应用程序我注意到,对数据库的连接一直失败,因为ConfigurationManager中保持返回的machine.config连接字符串:
I have a c# assembly that uses the app.config to store its database connection string. When debugging the application I noticed that the connection to the database kept failing because the ConfigurationManager kept returning the machine.config connection string:
数据源= .\SQLEXPRESS;集成的安全性; ....
data source=.\SQLEXPRESS; Integrated Security;....
我添加<在应用我的连接字符串前清除/
>。配置和它固定我的dev的机器上的问题。当我部署它的生产问题返回。谁能告诉我,我怎么能停止使用在machine.config连接字符串?
I added <clear/
> before my connection string in the app.config and it fixed the issue on my dev machine. The problem returned when I deployed it to production. Can someone tell me how I can stop the machine.config connection string from being used?
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[0].ConnectionString);
<connectionStrings>
<clear/>
<add name="VersionConnectionString"
connectionString=" Data Source=localhost;Initial Catalog=VersionInfo;User ID=user;Password=password"
providerName="System.Data.SqlClient" />
</connectionStrings>
更新
UPDATE
下面仍然给我?!在machine.config连接字符串
The following still gives me the machine.config connection string?!
Configuration appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
string dllConfigData =
appConfig.ConnectionStrings.ConnectionStrings[0].ConnectionString;
谢谢!
Thanks!
推荐答案
当在DLL中使用连接字符串,你需要将它们添加到您的exe的app.config为好,使用该设置完全相同的名称。然后,您可以更改连接字符串中的exe文件的config文件并加载时,DLL会自动把它捡起来。
When using connection strings in a DLL, you need to add them to your exe's app.config as well, using the exact same name for the setting. Then, you can change the connection string in the exe's .config file and the DLL will pick it up automatically when loaded.
这可能是你可以有工作的唯一途径在app.config文件中自定义的连接字符串时,你的数据库持久层在一个单独的DLL中实现。甚至不要问我多少时间带我去研究和调试此。
This is probably the only way you can have working custom connection strings in the app.config file when your DB persistence layer is implemented in a separate DLL. Don't even ask me how much time it took me to research and debug this.
这篇关于ConfigurationManager中不断获得的Machine.config连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!