我已经创建了自己的配置部分,但我已陷入死胡同。我继续获得Nullreference
- 例外。我使用以下Guide创建了自己的配置部分。
显而易见的是Nothing
,但我无法查看/找到它是什么。由于我在调试方面的漏洞,知识无法进一步调试。有人能指出我做错了什么吗?
App.Config(我的自定义部分DatabaseConnections):
<configuration>
<configSections>
<section name="DatabaseConnections" type="TaskDatabase.Common.ConfigurationSections.ConnectionstringsConfigSection,TaskDatabase.Common"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<DatabaseConnections>
<Connectionstrings>
<add ConnName="TaskDatabase" ConnString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source="C:\...\TaskDB.accdb""></add>
</Connectionstrings>
</DatabaseConnections>
</configuration>
我的配置代码:
public class ConnectionstringsConfigSection : System.Configuration.ConfigurationSection
{
public const string DbConnectionsSectionName = "DatabaseConnections";
private const string ConnectionStringsCollectionName = "Connectionstrings";
[ConfigurationProperty(DbConnectionsSectionName, IsDefaultCollection = false)]
public ConnectionStringsCollection ConnectionStrings
{
get
{
return (ConnectionStringsCollection)base[ConnectionStringsCollectionName];
}
}
}
异常发生在base[ConnectionStringsCollectionName]
,返回nullReference
。我无法执行Step Into
,这就是为什么我有点卡住了。有人能告诉我我做错了什么或我如何进一步调试它?
StackTrace:
bij System.Configuration.ConfigurationElement.get_Item(String propertyName)
bij TaskDatabase.Common.ConfigurationSections.ConnectionstringsConfigSection.get_ConnectionStrings() in ....
ConnectionStringCollection - Class
public class ConnectionStringsCollection : ConfigurationElementCollection
{
public ConnectionString this[int index]
{
get
{
return base.BaseGet(index) as ConnectionString;
}
set
{
if (base.BaseGet(index) != null)
{
base.BaseRemoveAt(index);
}
this.BaseAdd(index, value);
}
}
public new ConnectionString this[string responseString]
{
get { return (ConnectionString)BaseGet(responseString); }
set
{
if (BaseGet(responseString) != null)
{
BaseRemoveAt(BaseIndexOf(BaseGet(responseString)));
}
BaseAdd(value);
}
}
protected override System.Configuration.ConfigurationElement CreateNewElement()
{
return new ConnectionString();
}
protected override object GetElementKey(System.Configuration.ConfigurationElement element)
{
return ((ConnectionString)element).NameProp;
}
}
配置 - 数据元素:
//ConfigurationElement
public class ConnectionString : ConfigurationElement
{
[ConfigurationProperty("ConnName", IsRequired = true)]
public string NameProp
{
get
{
return this["ConnName"] as string;
}
}
[ConfigurationProperty("ConnString", IsRequired = true)]
public string ConnectionstringProp
{
get
{
return this["ConnString"] as string;
}
}
}
那么有人能发现我所犯的错误吗?
感谢您的时间。
答案 0 :(得分:0)
检查一下:
您的连接字符串包含:
"
C:\ Users \ Stijn \ Documents \ Visual Studio 2012 \ Projects \ TaskDatabase \ TaskDatabase \ TaskDB.accdb "
;
检查&#34;在连接字符串的开头和结尾。
尝试删除它,也许你从其他地方复制这部分;)
我希望这会有所帮助