无法从配置管理器中提取连接字符串
我知道我只是错过了一些简单的东西,但是当我尝试从 ConfigurationManager 中提取连接字符串时,我总是得到 null。
我已添加 System.configuration 作为参考
在源文件中我
using System.Configuration;
using MySql.Data;
using MySql.Data.MySqlClient;
....
_connStr = ConfigurationManager.ConnectionStrings["MySqlDataConnection"].ConnectionString;
objConnection = new MySqlConnection(_connStr);
在应用程序配置中的位置...
<configuration>
<connectionStrings>
<add name="MySqlDataConnection"
connectionString="server=127.0.0.1;database=HLSDB;uid=me;password=myPasswd;pooling=false;"
providerName="MySql.Data.SqlClient"/>
</connectionStrings>
</configuration>
I know I just missing something simple, but when I try pulling the connection string from the ConfigurationManager, I always get null.
I have System.configuration added as a reference
In the source file I have
using System.Configuration;
using MySql.Data;
using MySql.Data.MySqlClient;
....
_connStr = ConfigurationManager.ConnectionStrings["MySqlDataConnection"].ConnectionString;
objConnection = new MySqlConnection(_connStr);
where in my app config I have...
<configuration>
<connectionStrings>
<add name="MySqlDataConnection"
connectionString="server=127.0.0.1;database=HLSDB;uid=me;password=myPasswd;pooling=false;"
providerName="MySql.Data.SqlClient"/>
</connectionStrings>
</configuration>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您可以尝试
验证您正在查看正确的配置文件。
Maybe you could try
To verify you are looking at the correct config file.
请仔细检查您的配置文件是否已在输出目录中部署为
MyApplication.exe.config
(而不是app.config
)。您发布的代码很好。Please double-check that your configuration file is deployed as
MyApplication.exe.config
(and notapp.config
) in the output directory. That code you have posted is fine.