如何修复“ConnectionString属性尚未初始化” [英] How to fix "The ConnectionString property has not been initialized"

查看:261
本文介绍了如何修复“ConnectionString属性尚未初始化”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我开始我的应用程序时,我得到: ConnectionString属性尚未初始化。

When I start my application I get: The ConnectionString property has not been initialized.

config:

<connectionStrings>
    <add name="MyDB"
         connectionString="Data Source=localhost\sqlexpress;Initial Catalog=mydatabase;User Id=myuser;Password=mypassword;" />
</connectionStrings>

堆栈为:

System.Data.SqlClient.SqlConnection.PermissionDemand() +4876643
System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection) +20
System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +117
System.Data.SqlClient.SqlConnection.Open() +122

我是.NET的新手,我没有得到这个。我在Google上找到了很多答案,但没有一个解决我的问题。

I'm fairly new to .NET and I don't get this one. I found a lot of answers on Google, but none really fixed my issue.

这是什么意思?我的web.config是坏?我的功能坏吗?是我的SQL配置不正常工作(我使用sqlexpress)?

What does that mean? Is my web.config bad? Is my function bad? Is my SQL configuration not working correctly (I'm using sqlexpress)?

我的主要问题是,我不知道在哪里开始调试这个...任何东西都会有帮助。

My main problem here is that I'm not sure where to start to debug this... anything would help.

编辑:

养育代码:

MySQLHelper.ExecuteNonQuery(
ConfigurationManager.AppSettings["ConnectionString"],
CommandType.Text,
sqlQuery,
sqlParams);

sqlQuery是一个类似select * from table的查询。 sqlParams在这里是不相关的。

sqlQuery is a query like "select * from table". sqlParams is not relevant here.

这里的另一个问题是,我的公司使用MySQLHelper,我没有可见性(只有一个帮助lib的dll)。它已经在其他项目中正常工作,所以我99%的错误不是来自这里。

The other problem here is that my company uses MySQLHelper, and I have no visibility over it (only have a dll for a helper lib). It has been working fine in other projects, so I'm 99% that the error doesn't come from here.

我想如果没有办法调试它,没有看到代码,我不得不等待联系创建这个帮手的人,以获得代码。

I guess if there's no way of debuging it without seeing the code I'll have to wait to get in touch with the person who created this helper in order to get the code.

推荐答案

引用连接字符串应该这样:

Referencing the connection string should be done as such:

MySQLHelper.ExecuteNonQuery(
ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString,
CommandType.Text,
sqlQuery,
sqlParams);

ConfigurationManager.AppSettings [ConnectionString] 将在 AppSettings 中查找名为 ConnectionString 的东西,它将找不到。这是为什么你的错误信息表明ConnectionString属性尚未初始化,因为它正在寻找一个初始化的属性 AppSettings 命名 ConnectionString

ConfigurationManager.AppSettings["ConnectionString"] would be looking in the AppSettings for something named ConnectionString, which it would not find. This is why your error message indicated the "ConnectionString" property has not been initialized, because it is looking for an initialized property of AppSettings named ConnectionString.

ConfigurationManager.ConnectionStrings [MyDB]。ConnectionString 指示寻找名为MyDB的连接字符串。

ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString instructs to look for the connection string named "MyDB".

这里有人谈论使用web.config连接字符串

这篇关于如何修复“ConnectionString属性尚未初始化”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆