3

データベースアクセスを必要とするいくつかの単純なライブラリを作成しようとしています。私が得られないのは、接続文字列をライブラリに渡す方法です...現在、dllにdb.configがありますが、dllなどから参照する方法がわかりません...

これが私がライブラリをセットアップした方法です

【ソリューションファイル】

  • Library1
    • db.config
  • Library2
    • リンクされたdb.config

<configuration>
        <!-- Connection string -->
    </configuration>
  1. dll内からdb.configを参照するにはどうすればよいですか?
  2. Webアプリケーションweb.configからdb.configを参照するにはどうすればよいですか?
4

2 に答える 2

1

本当にシンプル:

1)これが.Net .dllである場合は、「app.config」または「web.config」に保存できます。

<?xml version="1.0" encoding="utf-8" ?>
  <configuration>
    <connectionStrings>
      <add name="DigiBrd"
           connectionString="server=localhost;user id=****;Password=****;database=DigiBrd"
           providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
</configuration>

String cnString =
  ConfigurationManager.ConnectionStrings["DigiBrd"].ConnectionString;

2)接続文字列は、.dllが読み取れる場所であればどこにでも保存できます。たとえば、.iniファイルまたはレジストリを使用できます。

3)getConnectionString().dllにメソッドを実装できます。

4)等等

于 2012-07-11T18:30:47.947 に答える
1

あなたの最初の質問に答えて:

私は通常、次のようなConfigurationManagerメソッドの1つを使用することを好みます。

http://msdn.microsoft.com/en-us/library/ms224437.aspx

または、XPathを使用した古いスタイルのXmlがあります。

XmlDocument webConfig = new XmlDocument();
webConfig.Load(dllConfigFileName);
XmlNode someNode = webConfig.SelectSingleNode("//configuration/appSettings/add[@key='someKey']");

または、新しいLINQ to XML:

XDocument document = XDocument.Load(configFileFullName);
XElement configurationElement = document.Element("configuration");
XElement appSettingsElement = configurationElement.Element("appSettings");
List<XElement> configSettings = new List<XElement>(appSettingsElement.Descendants("add"));
于 2012-07-11T18:43:26.970 に答える