Csharp/C#教程:ConnectionString属性尚未初始化错误分享


ConnectionString属性尚未初始化错误

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

 

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

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

出于某种原因,我得到错误:

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

堆栈跟踪是:

 [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 

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

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

我试图使用:

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

但我需要使用:

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

将连接字符串更改为

     

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

上述就是C#学习教程:ConnectionString属性尚未初始化错误分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

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

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/989429.html

(0)
上一篇 2021年12月23日
下一篇 2021年12月23日

精彩推荐