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

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

问题描述

当我开始我的申请,我得到: ConnectionString属性尚未初始化

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

Web.config文件:

Web.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和我没有得到这一个。我发现了很多关于谷歌的答案,但没有真正固定我的问题。

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配置正确(我使用sqlex preSS)?

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.

编辑:

Failling code:

Failling code:

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

的SQLQuery就像是SELECT * FROM表查询。 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.

我想,如果没有debuging它没有看到code的方式,我将不得不等待与谁才能获得code创建这个助手的人取得联系。

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".

<一个href=\"http://weblogs.asp.net/owscott/archive/2005/08/26/Using-connection-strings-from-web.config-in-ASP.NET-v2.0.aspx\">Here是有人在谈论使用的web.config连接字符串

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

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