C# 如何修复“ConnectionString 属性尚未初始化”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1007786/
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
How to fix "The ConnectionString property has not been initialized"
提问by marcgg
When I start my application I get: The ConnectionString property has not been initialized.
当我启动我的应用程序时,我得到: ConnectionString 属性尚未初始化。
Web.config:
网页配置:
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=localhost\sqlexpress;Initial Catalog=mydatabase;User Id=myuser;Password=mypassword;" />
</connectionStrings>
The stack being:
堆栈是:
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
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.
我对 .NET 还很陌生,我没有得到这个。我在谷歌上找到了很多答案,但没有一个能真正解决我的问题。
What does that mean? Is my web.config bad? Is my function bad? Is my SQL configuration not working correctly (I'm using sqlexpress)?
这意味着什么?我的 web.config 不好吗?我的功能不好吗?我的 SQL 配置是否工作不正常(我正在使用 sqlexpress)?
My main problem here is that I'm not sure where to start to debug this... anything would help.
我的主要问题是我不确定从哪里开始调试这个......任何事情都会有所帮助。
EDIT:
编辑:
Failling code:
失败代码:
MySQLHelper.ExecuteNonQuery(
ConfigurationManager.AppSettings["ConnectionString"],
CommandType.Text,
sqlQuery,
sqlParams);
sqlQuery is a query like "select * from table". sqlParams is not relevant here.
sqlQuery 是一个类似于“select * from table”的查询。sqlParams 在这里不相关。
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.
这里的另一个问题是我的公司使用 MySQLHelper,我无法看到它(只有一个帮助程序库的 dll)。它在其他项目中运行良好,所以我 99% 认为错误不是来自这里。
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.
我想如果没有看到代码就无法调试它,我将不得不等待与创建此帮助程序的人取得联系才能获得代码。
采纳答案by kscott
Referencing the connection string should be done as such:
应按如下方式引用连接字符串:
MySQLHelper.ExecuteNonQuery(
ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString,
CommandType.Text,
sqlQuery,
sqlParams);
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.AppSettings["ConnectionString"]
将寻找AppSettings
名为 的东西ConnectionString
,但它找不到。这就是您的错误消息指出 的"ConnectionString" property has not been initialized
原因,因为它正在寻找AppSettings
named的初始化属性ConnectionString
。
ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString
instructs to look for the connection string named "MyDB".
ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString
指示查找名为“MyDB”的连接字符串。
Here is someone talking about using web.config connection strings
回答by Matthew Jones
You get this error when a datasource attempts to bind to data but cannot because it cannot find the connection string. In my experience, this is not usually due to an error in the web.config (though I am not 100% sure of this).
当数据源尝试绑定到数据但由于找不到连接字符串而无法绑定时,您会收到此错误。根据我的经验,这通常不是由于 web.config 中的错误(尽管我不是 100% 确定)。
If you are programmatically assigning a datasource (such as a SqlDataSource) or creating a query (i.e. using a SqlConnection/SqlCommand combination), make sure you assigned it a ConnectionString.
如果您以编程方式分配数据源(例如 SqlDataSource)或创建查询(即使用 SqlConnection/SqlCommand 组合),请确保为其分配了 ConnectionString。
var connection = new SqlConnection(ConfigurationManager.ConnectionStrings[nameOfString].ConnectionString);
If you are hooking up a databound element to a datasource (i.e. a GridView or ComboBox to a SqlDataSource), make sure the datasource is assigned to one of your connection strings.
如果您将数据绑定元素连接到数据源(即 GridView 或 ComboBox 到 SqlDataSource),请确保将数据源分配给您的连接字符串之一。
Post your code (for the databound element and the web.config to be safe) and we can take a look at it.
发布你的代码(为了数据绑定元素和 web.config 是安全的),我们可以看看它。
EDIT:I think the problem is that you are trying to get the Connection String from the AppSettings area, and programmatically that is not where it exists. Try replacing that with ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString
(if ConnectionString is the name of your connection string.)
编辑:我认为问题在于您试图从 AppSettings 区域获取连接字符串,并且以编程方式不存在它。尝试将其替换为ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString
(如果 ConnectionString 是您的连接字符串的名称。)
回答by mbillard
The connection string is notin AppSettings.
连接字符串是不是在AppSettings的。
What you're looking for is in:
您正在寻找的是:
System.Configuration.ConfigurationManager.ConnectionStrings["MyDB"]...
回答by Fadao
Use [] instead of () as below example.
使用 [] 而不是 () 如下例所示。
SqlDataAdapter adapter = new SqlDataAdapter(sql, ConfigurationManager.ConnectionStrings["FADB_ConnectionString"].ConnectionString);
DataTable data = new DataTable();
DataSet ds = new DataSet();
回答by Priyank Raunak
Simply in Code Behind Page use:-
只需在代码隐藏页面中使用:-
SqlConnection con = new SqlConnection("Data Source = DellPC; Initial Catalog = Account; user = sa; password = admin");
It Should Work Just Fine
它应该可以正常工作
回答by Cerini Pablo
I stumbled in the same problem while working on a web api Asp Net Core project. I followed the suggestion to change the reference in my code to:
我在处理 web api Asp Net Core 项目时遇到了同样的问题。我按照建议将代码中的引用更改为:
ConfigurationManager.ConnectionStrings["NameOfTheConnectionString"].ConnectionString
but adding the reference to System.Configuration.dll caused the error "Reference not valid or not supported".
但是添加对 System.Configuration.dll 的引用会导致错误“引用无效或不受支持”。
To fix the problem I had to download the package System.Configuration.ConfigurationManager using NuGet (Tools -> Nuget Package-> Manage Nuget packages for the solution)
为了解决这个问题,我必须使用 NuGet 下载包 System.Configuration.ConfigurationManager(工具 -> Nuget 包 -> 管理 Nuget 包作为解决方案)