次のようなカスタム構成セクションがあります
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="DBConfiguration" type="NewSQLExecuter.DBConfigurationSection, NewSQLExecuter"/>
</configSections>
<DBConfiguration>
<Items>
<add servername="192.168.50.2\db1" dbname="test1" userid="sa" password="2222m@n" countrycode="GB" />
<add servername="192.168.60.2\db2" dbname="test2" userid="sa" password="22222n" countrycode="US" />
<add servername="192.168.70.2\db3" dbname="test3" userid="sa" password="3333" countrycode="DE" />
</Items>
</DBConfiguration>
</configuration>
カスタム構成セクションからデータを読み取るクラスを作成しました...クラスコードは次のとおりです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace NewSQLExecuter
{
public class DBConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("Items")]
public ItemsCollection Items
{
get { return ((ItemsCollection)(base["Items"])); }
}
}
[ConfigurationCollection(typeof(ItemsElement))]
public class ItemsCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ItemsElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ItemsElement)(element)).CountryCode;
}
public ItemsElement this[int idx]
{
get
{
return (ItemsElement)BaseGet(idx);
}
}
}
public class ItemsElement : ConfigurationElement
{
[ConfigurationProperty("servername", DefaultValue = "", IsKey = false, IsRequired = false)]
public string ServerName
{
get
{
return ((string)(base["servername"]));
}
set
{
base["servername"] = value;
}
}
[ConfigurationProperty("dbname", DefaultValue = "", IsKey = false, IsRequired = false)]
public string DBName
{
get
{
return ((string)(base["dbname"]));
}
set
{
base["dbname"] = value;
}
}
[ConfigurationProperty("userid", DefaultValue = "", IsKey = false, IsRequired = false)]
public string UserID
{
get
{
return ((string)(base["userid"]));
}
set
{
base["userid"] = value;
}
}
[ConfigurationProperty("password", DefaultValue = "", IsKey = false, IsRequired = false)]
public string Password
{
get
{
return ((string)(base["password"]));
}
set
{
base["password"] = value;
}
}
[ConfigurationProperty("countrycode", DefaultValue = "", IsKey = true, IsRequired = true)]
public string CountryCode
{
get
{
return ((string)(base["countrycode"]));
}
set
{
base["countrycode"] = value;
}
}
}
}
このようにして、カスタム構成データを反復して値を取得します
DBConfigurationSection section = (DBConfigurationSection)ConfigurationManager.GetSection("DBConfiguration");
if (section != null)
{
DateTime satrt = DateTime.Now;
for (int i = 0; i <= section.Items.Count - 1; i++)
{
var country = section.Items[i].CountryCode; ;
var constring = string.Format("{0}{1}{2}{3}", "UID=" + section.Items[i].UserID, ";PWD=" + section.Items[i].Password,
";Server=" + section.Items[i].ServerName, ";Database=" + section.Items[i].DBName);
dicList.Add(country, constring);
}
}
コードは正常に動作しますが、次のような任意のキー値で構成データを検索するなどの機能をもう 1 つ追加したいと思います
if(section.key.userid=="joy")
{
string pwd=section.key.password
}
クラスに検索機能を追加する方法を教えてください。LINQ を使用してカスタム構成データを検索できればいいのですが。ありがとうございます。
app.config ファイルの 2 つのカスタム構成セクションを処理する際にエラーが発生する
私のapp.configファイルは次のようになります
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="DBConfiguration" type="CSRAssistant.DBConfigurationSection, CSRAssistant"/>
<section name="LoginConfiguration" type="CSRAssistant.LoginConfigurationSection, CSRAssistant"/>
</configSections>
<DBConfiguration>
<add servername="dbname" dbname="BBAJobBoardForGB" userid="sa" password="222" countrycode="GBR" />
<add servername="db2" dbname="BBAJobBoardForUS" userid="sa" password="swww" countrycode="USA" />
</DBConfiguration>
<LoginConfiguration>
<add UserName="ww" Pwd="ww" Country="GBR"/>
<add UserName="ss" Pwd="ss" Country="USA"/>
<add UserName="dd" Pwd="dd" Country="CAD"/>
</LoginConfiguration>
<appSettings>
<add key="MailID" value="tridip@bba-reman.com" />
</appSettings>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
<requiredRuntime version="v4.0.20506"/>
</startup>
</configuration>
2つのセクションを処理するには、私のコードは次のようになります
namespace CSRAssistant
{
public class DBConfigurationSection : ConfigurationSection
{
//[ConfigurationProperty("Items")]
//public ItemsCollection Items
//{
// get { return ((ItemsCollection)(base["Items"])); }
//}
[ConfigurationProperty("", IsDefaultCollection = true)]
public ItemsCollection Items
{
get
{
return ((ItemsCollection)(base[""]));
}
}
}
[ConfigurationCollection(typeof(ItemsElement))]
public class ItemsCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ItemsElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ItemsElement)(element)).CountryCode;
}
public ItemsElement this[int idx]
{
get
{
return (ItemsElement)BaseGet(idx);
}
}
}
public class ItemsElement : ConfigurationElement
{
[ConfigurationProperty("servername", DefaultValue = "", IsKey = false, IsRequired = false)]
public string ServerName
{
get
{
return ((string)(base["servername"]));
}
set
{
base["servername"] = value;
}
}
[ConfigurationProperty("dbname", DefaultValue = "", IsKey = false, IsRequired = false)]
public string DBName
{
get
{
return ((string)(base["dbname"]));
}
set
{
base["dbname"] = value;
}
}
[ConfigurationProperty("userid", DefaultValue = "", IsKey = false, IsRequired = false)]
public string UserID
{
get
{
return ((string)(base["userid"]));
}
set
{
base["userid"] = value;
}
}
[ConfigurationProperty("password", DefaultValue = "", IsKey = false, IsRequired = false)]
public string Password
{
get
{
return ((string)(base["password"]));
}
set
{
base["password"] = value;
}
}
[ConfigurationProperty("countrycode", DefaultValue = "", IsKey = true, IsRequired = true)]
public string CountryCode
{
get
{
return ((string)(base["countrycode"]));
}
set
{
base["countrycode"] = value;
}
}
}
public class LoginConfigurationSection : ConfigurationSection
{
//[ConfigurationProperty("Items")]
//public ItemsCollection Items
//{
// get { return ((ItemsCollection)(base["Items"])); }
//}
[ConfigurationProperty("", IsDefaultCollection = true)]
public ItemsCollection Items
{
get
{
return ((ItemsCollection)(base[""]));
}
}
}
public class LoginElement : ConfigurationElement
{
[ConfigurationProperty("UserName", DefaultValue = "", IsKey = false, IsRequired = false)]
public string UserName
{
get
{
return ((string)(base["UserName"]));
}
set
{
base["UserName"] = value;
}
}
[ConfigurationProperty("Pwd", DefaultValue = "", IsKey = false, IsRequired = false)]
public string Pwd
{
get
{
return ((string)(base["Pwd"]));
}
set
{
base["Pwd"] = value;
}
}
[ConfigurationProperty("Country", DefaultValue = "", IsKey = false, IsRequired = false)]
public string Country
{
get
{
return ((string)(base["Country"]));
}
set
{
base["Country"] = value;
}
}
}
}
このように2つのカスタム構成設定を読み取ります
string joyPassword = "";
LoginConfigurationSection LoginConfigurationSection = (LoginConfigurationSection)ConfigurationManager.GetSection("LoginConfiguration");
if (LoginConfigurationSection != null)
{
var UserCredentials = LoginConfigurationSection.Items
.Cast<LoginElement>()
.FirstOrDefault(_element => _element.UserName == "razi");
if (UserCredentials != null)
joyPassword = UserCredentials.Country;
DBConfigurationSection section = (DBConfigurationSection)ConfigurationManager.GetSection("DBConfiguration");
if (section != null)
{
var DbConnection = section.Items
.Cast<ItemsElement>()
.FirstOrDefault(_element => _element.CountryCode.ToUpper() == joyPassword.ToUpper());
if (DbConnection != null)
joyPassword = DbConnection.ServerName;
}
}
今、実行時に認識されない属性 'UserName' のようなエラーが発生しています 。属性名は大文字と小文字が区別されることに注意してください。
認識されない属性「UserName」が理解できない理由....可能であれば、私を案内してください。ありがとう