我需要创建一个嵌套的配置部分。问题是我需要编写一个访问任意数量数据库的应用程序。这些可能是 oracle、sql 或其他任何东西......我想让我的配置部分看起来像这样:
<connectionconfigurations>
<databaseConnection dbname="connection1" dbsourceConnect="connectionstring1" provider="sql">
<sqlQueries>
<add name="querynumber1"
sqlFilePath="C:\Users\me\Desktop\sql"/>
<add name="querynumber2"
sqlFilePath="C:\Users\me\Desktop\sql"/>
</sqlQueries>
</databaseConnection>
<databaseConnection dbname="connection1" dbsourceConnect="connectionstring2" provider="oracle">
<sqlQueries>
<add name="querynumber3"
sqlFilePath="C:\Users\me\Desktop\oracle"/>
<add name="querynumber4"
sqlFilePath="C:\Users\me\Desktop\oracle"/>
</sqlQueries>
</databaseConnection>
</connectionconfigurations>
问题是我无法访问所有参数。如何创建这样的嵌套配置部分并通过代码访问它们?
我做了一个这样的类来处理连接部分:
public class Connectionconfigurations : ConfigurationSection
{
private static ConfigurationPropertyCollection _connectionConfiguration;
private static ConfigurationPropertyCollection _sqlQueries;
private static ConfigurationPropertyCollection _properties;
static Connectionconfigurations()
{
_connectionConfiguration = new ConfigurationProperty(
ConfigConstants.CONFIG_DATABASECONNECTION,
typeof(DatabaseConnectionElementCollection),
null,
ConfigurationPropertyOptions.IsRequired);
_sqlQueries = new ConfigurationProperty(
ConfigConstants.CONFIG_SQLQUERIES,
typeof(DatabaseConnectionElementCollection),
null,
ConfigurationPropertyOptions.IsRequired);
_properties = new ConfigurationPropertyCollection();
// Add other properties
_properties.Add(_databaseConnection);
}
[ConfigurationProperty("databaseConnection")]
public DatabaseConnectionElementCollection DatabaseConnection
{
get { return (DatabaseConnectionElementCollection)base[_databaseConnection]; }
}
}
但是,我收到错误消息:“无法识别的属性 'dbname'。请注意,属性名称区分大小写。”
数据库连接元素类:
public class DatabaseConnectionElement : ConfigurationElement
{
private static ConfigurationProperty _name;
private static ConfigurationProperty _sourceConnect;
private static ConfigurationProperty _provider;
private static SqlQueryElementCollection _sqlCollection; // Collection of sql queries for this DB element
private static ConfigurationPropertyCollection _properties;
static DatabaseConnectionElement()
{
_name = new ConfigurationProperty(ConfigConstants.CONFIG_DB_NAME, typeof(string), string.Empty,
ConfigurationPropertyOptions.IsKey);
_sourceConnect = new ConfigurationProperty(ConfigConstants.CONFIG_DB_SOURCECONNECT, typeof(string), string.Empty,
ConfigurationPropertyOptions.IsRequired);
_provider = new ConfigurationProperty(ConfigConstants.CONFIG_DB_PROVIDER, typeof(string), string.Empty,
ConfigurationPropertyOptions.IsRequired);
_sqlCollection = new SqlQueryElementCollection();
_properties = new ConfigurationPropertyCollection();
_properties.Add(_name);
_properties.Add(_sourceConnect);
_properties.Add(_reconnectDelay);
}
[ConfigurationProperty("dbname", IsKey = true)]
public string Name
{
get
{
return (string)base[_name];
}
}
[ConfigurationProperty("dbsourceConnect", IsRequired = true)]
public string SourceConnect
{
get
{
return (string)base[_sourceConnect];
}
}
[ConfigurationProperty("provider", IsRequired = true)]
public string Provider
{
get
{
return (string)base[_provider];
}
}
protected override ConfigurationPropertyCollection Properties
{
get
{
return _properties;
}
}
}