【发布时间】:2011-05-16 09:45:08
【问题描述】:
我正在尝试打这个简单的电话:
DataContext dc = new DataContext(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString)
这是我的 app.config 文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="MyDB" connectionString="Server=STEVEN-PC;Database=MyDB;Trusted_Connection=yes;" />
</connectionStrings>
</configuration>
但我收到一个错误:对象引用未设置为对象的实例。
找不到连接字符串。我做错了什么?
【问题讨论】:
-
确定您的 app.config 与可执行文件在同一路径中?
-
@Inuyasha - 我有一个包含多个项目的 Visual Studio 解决方案。 app.config 和我从中调用的文件在同一个项目中。
-
我总是喜欢通过将 app.config 的调用与实例化 dataContext 分开来确认我的假设。只是为了确保那是我失败的地方。你可能是对的,但它时不时地揭示出一些意想不到的东西。
-
你提到有几个项目。 app.config 是你的启动项目吗?如果没有,您必须将连接字符串添加到启动项目中的 app.config。