C# ConnectionString 属性尚未初始化错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19355946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:49:24  来源:igfitidea点击:

The ConnectionString property has not been initialized error

c#mysqlasp.netconnection-stringinvalidoperationexception

提问by Matan Kadosh

I'm using an asp.net web application with a MySql DB, with the following connetcion string the web.config:

我正在使用带有 MySql DB 的 asp.net Web 应用程序,并在 web.config 中使用以下连接字符串:

<add name="techConnectionString" connectionString="server=vm-tmysql01;User Id=user;database=tech;password=pass" providerName="MySql.Data.MySqlClient"/>

And this is the code I'm using the get the connection string from the web.config:

这是我使用从 web.config 获取连接字符串的代码:

System.configuration.configurationmanaget.connectionstrings["techConnectionString"].connectionstring;

For some reason I get the error:

出于某种原因,我收到错误消息:

The ConnectionString property has not been initialized. An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

ConnectionString 属性尚未初始化。执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常来源和位置的信息。

And the stack trace is:

堆栈跟踪是:

[InvalidOperationException: The ConnectionString property has not been initialized.]
   System.Data.SqlClient.SqlConnection.PermissionDemand() +6316916
   System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +6296606
   System.Data.SqlClient.SqlConnection.Open() +300

I'm able to connect to the DB via the Data Sources in Visual Studio and took the ConnectionString from there. I really need some help with it, is there any problem with my connection string?

我可以通过 Visual Studio 中的数据源连接到数据库,并从那里获取 ConnectionString。我真的需要一些帮助,我的连接字符串有问题吗?

回答by Damith

change connection string as

将连接字符串更改为

 <connectionStrings>
    <add name="techConnectionString" connectionString="SERVER=vm-tmysql01; DATABASE=tech; UID=user; 
PASSWORD=pass" providerName="MySql.Data.MySqlClient"/>
 </connectionStrings>

and you can get connection using System.Configuration( you need to add reference to System.Configuration)

并且您可以使用System.Configuration(您需要添加对System.Configuration)的连接

var constring =ConfigurationManager.ConnectionStrings["techConnectionString"].ConnectionString;

回答by Matan Kadosh

The problem wasn't with the connetion string, it was with the c# code trying to reach it from the web.config file.

问题不在于连接字符串,而在于尝试从 web.config 文件访问它的 c# 代码。

i was trying to use:

我试图使用:

System.Configuration.ConfigurationManager.ConnectionStrings["techConnectionString"].ConnectionString;

But i needed to use:

但我需要使用:

System.Web.Configuration.WebConfigurationManager.ConnectionStrings["techConnectionString"].ConnectionString;