问题描述
我有一个简单的WinForm,我用来尝试帮助我与ADO.NET。
它有一个datagridview
I have a simple WinForm that I'm using to try to help me with ADO.NET.It has a datagridview
我添加了一个紧凑的sql服务器数据库到项目 experiment.sdf
I have added a compact sql server database to the project called experiment.sdf
在App.config文件中我添加了以下内容。我使用一个以前的项目应用程序文件,以此为基础,所以也许这里有一个错误:
In the App.config file I have added the following. I used a previous project app file to base this on so maybe there's an error in here?:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="DatabaseDGVexperiments.Properties.Settings.DatabaseDGVexperimentsConnStg"
connectionString="Data Source=|DataDirectory|\experiment.sdf"
providerName="Microsoft.SqlServerCe.Client.3.5" />
</connectionStrings>
</configuration>
我已经添加了对配置库的引用。
I've added a reference to the configuration library.
后面的代码如下:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseDGVexperimentsConnStg"].ConnectionString);
当它命中行 SqlConnection conn = ...
我得到一个错误 NullReferenceException未处理
。如何解决此问题?
When it hits the line SqlConnection conn = ...
I get an error NullReferenceException was unhandled
. How do I fix this?
推荐答案
更改:
name="DatabaseDGVexperiments.Properties.Settings.DatabaseDGVexperimentsConnStg"
name="DatabaseDGVexperimentsConnStg"
或更改:
ConfigurationManager.ConnectionStrings["DatabaseDGVexperimentsConnStg"]
到:
ConfigurationManager.ConnectionStrings["DatabaseDGVexperiments.Properties.Settings.DatabaseDGVexperimentsConnStg"]
这篇关于非常简单的C#程序生成一个`NullReferenceException是未处理的'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!