从 App.config 获取连接字符串
var connection = ConnectionFactory.GetConnection(
ConfigurationManager.ConnectionStrings["Test"]
.ConnectionString, DataBaseProvider);
这是我的 App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Test" connectionString="Data Source=.;Initial Catalog=OmidPayamak;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
但是当我的项目运行时,这是我的错误:
未将对象引用设置为对象的实例。
var connection = ConnectionFactory.GetConnection(
ConfigurationManager.ConnectionStrings["Test"]
.ConnectionString, DataBaseProvider);
And this is my App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Test" connectionString="Data Source=.;Initial Catalog=OmidPayamak;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
But when my project runs this is my error:
Object reference not set to an instance of an object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(22)
您可以执行以下操作:
您的程序集还需要对 System.Configuration.dll 的引用
You can just do the following:
Your assembly also needs a reference to
System.Configuration.dll
由于这是一个非常常见的问题,我准备了一些 Visual Studio 的屏幕截图,以便通过 4 个简单的步骤轻松理解。
Since this is very common question I have prepared some screen shots from Visual Studio to make it easy to follow in 4 simple steps.
另请检查您是否已在引用下包含
System.Configuration
dll。如果没有它,您将无法访问 System.Configuration 命名空间中的ConfigurationManager
类。Also check that you've included the
System.Configuration
dll under your references. Without it, you won't have access to theConfigurationManager
class in the System.Configuration namespace.首先将
System.Configuration
的引用添加到您的页面。然后根据您的 app.config 获取连接字符串,如下所示。
现在您已经有了连接字符串并且可以使用它了。
First Add a reference of
System.Configuration
to your page.Then According to your app.config get the connection string as follow.
That's it now you have your connection string in your hand and you can use it.
试试这个
Try this out
这对我有用:
输出:
This worked for me:
Outputs:
这个问题中的OP可能试图在dll中使用App.Config。
在这种情况下,代码实际上是在尝试访问可执行文件的 App.Config,而不是 dll。由于未找到名称,因此返回 Null,因此显示异常。
以下帖子可能会有所帮助:
DLL 的 app.config 中的 ConnectionString 为 null
It is possible that the OP in this question is trying to use an App.Config within a dll.
In this case, the code is actually trying to access the App.Config of the executable and not the dll. Since the name is not found, you get a Null returned, hence the exception shown.
The following post may be helpful:
ConnectionString from app.config of a DLL is null
1)创建一个新表单并添加以下内容:
然后当您想要使用连接时,在您的表单中执行此操作:
就是这样。您将连接到数据库并且可以做一些事情。
这是针对 vb.net 的,但逻辑是相同的。
1) Create a new form and add this:
then when you want to use the connection do this in ur form:
and thats it. You will be connected to the DB and can do stuff.
This is for vb.net but the logic is the same.
你有没有尝试过:
Have you tried:
我有同样的问题。我的解决方案是由两个项目构建的。一个
类库
和一个引用该类库项目的网站。问题是我试图访问我的类库
项目中的App.config
,但系统正在搜索Web.config
的网站。我将连接字符串放入Web.config
中并且...问题解决了!主要原因是尽管
ConfigurationManager
在另一个程序集中使用,但它正在 runnig 项目内部进行搜索。I had the same Issue. my solution was built up from two projects. A
Class library
and a website referencing to the class library project. the problem was that i was trying to access theApp.config
in myClass library
project but the system was searching inWeb.config
of the website. I put the connection string insideWeb.config
and ... problem solved!The main reason was that despite
ConfigurationManager
was used in another assembly it was searching inside the runnig project .上面的答案没有详细说明connectionStrings索引中的值来自哪里。。
如上所述,要获取连接字符串,您可以说:
要使用
XXX
的正确值,请转到主项目web.config
文件并查找以下内容代码:其中显示
name=
,这些引号中的文本是您在上面的代码中使用的XXX
的值。在我上面的示例中,它恰好是身份验证
The answers above didn't elaborate where the value in connectionStrings index comes from.
As mentioned above, to get your connection string, you say:
To use the correct value of
XXX
, go to your main projectsweb.config
file and look for the following piece of code:Where it says
name=
, the text within those proceeding quotes is the value ofXXX
you use in the code above. In my example above, it happens to beAuthentication
问题似乎不在于引用,您将连接字符串设置为空,因此请确保您已将值添加到正在运行的项目的配置文件中,这意味着首先启动/执行的主程序/库。
It seems like problem is not with reference, you are getting connectionstring as null so please make sure you have added the value to the config file your running project meaning the main program/library that gets started/executed first.
首先,您必须向项目添加 System.Configuration 引用,然后使用以下代码获取连接字符串。
First you have to add
System.Configuration
reference to your project and then use below code to get connection string.您可以使用此方法来获取连接字符串
You can use this method to get connection string
您可以使用以下代码行获取连接字符串 -
这是参考:
连接来自 App.config 的字符串
You can fetch the connection string by using below line of code -
Here is a reference :
Connection String from App.config
我引用了 System.Configuration 库,并且出现了相同的错误。
调试文件没有 app.config,请手动创建此文件。
错误是,我通过在调试文件夹中复制文件“appname.exe.config”解决了这个问题。 IDE 未创建该文件。
I referenced
System.Configuration
library and I have the same error.The debug files had not their app.config, create manually this file.
The error is, I solved this copying the file "appname.exe.config" in debug folder. The IDE was not create the file.
我通过使用索引读取字符串并逐一检查来解决该问题。使用名称读取仍然会出现相同的错误。
我在开发 C# 窗口应用程序时遇到问题,但在我的 asp.net 应用程序中没有遇到问题。环境中一定有什么地方不对劲。
I solved the problem by using the index to read the string and checking one by one. Reading using the name still gives the same error.
I have the problem when I develop a C# window application, I did not have the problem in my asp.net application. There must be something in the setting which is not right.
为了从app.cfg(在Windows服务应用程序中)读取连接字符串,下面的代码对我有用
In order to read the connection string from app.cfg (in windows service application) the below code worked for me
在 UserControl 的构造函数中放置
ConfigurationManager.ConnectionStrings
时遇到此问题,这里的解决方案都不适合我。经过一番研究,在尝试从
DesignTime
访问它时,ConfigurationManager.ConnectionStrings
似乎将为 null。从这篇文章中,我想出了以下代码来检测
DesignTime
作为解决方法问题:Encountered this issue while placing
ConfigurationManager.ConnectionStrings
in UserControl's constructor, and none of the solution here worked for me.After some research, seems like
ConfigurationManager.ConnectionStrings
would be null while trying to access it from theDesignTime
.And from this post, I came up with this following code to detect
DesignTime
as a workaround to the issue: