gpt4 book ai didi

c# - ConnectionString 属性尚未初始化错误

转载 作者:太空宇宙 更新时间:2023-11-03 12:22:14 26 4
gpt4 key购买 nike

我正在使用带有 MySql 数据库的 asp.net web 应用程序,具有以下连接字符串 web.config:

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

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

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

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

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.

堆栈跟踪是:

[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。我真的需要一些帮助,我的连接字符串有问题吗?

最佳答案

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

我尝试使用:

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

但我需要使用:

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

关于c# - ConnectionString 属性尚未初始化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19355946/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com