将连接字符串传递给dll?
我想做一些简单的库,需要数据库访问。我没有得到的是如何将连接字符串传递给库...现在我在我的dll中有一个db.config,但我不知道如何从dll等引用它...将连接字符串传递给dll?
这是我已经设置了图书馆
[解决方案文件]
- 分享帮助
- db.config
- Library2
- 联db.config
<configuration>
<!-- Connection string -->
</configuration>
- 如何引用db.config从DLL中?
- 如何从Web应用程序web.config中引用db.config?
真简单:
1)如果发生这种情况是一个净.dll文件,你可以将其存储在应用程序的 “的app.config” 或 “web.config中”:
实例:
-
http://www.dreamincode.net/forums/topic/45321-grabbing-connectionstring-from-appconfig/
<?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)您可以在.dll中实现“getConnectionString()”方法。
4)等等等等
在回答你的第一个问题:
我通常喜欢使用的ConfigurationManager中的方法之一是这样一个:
http://msdn.microsoft.com/en-us/library/ms224437.aspx
或者有旧风格的XML使用XPath:
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"));
是在库或引用库的应用程序中引用的配置文件?现在我的应用程序和类库中都有一个conStrings.config文件,我使用字符串connString = ConfigurationManager.ConnectionStrings [“DBConnectMain”]调用它。ToString();但这似乎并不像一个伟大的想法。 – chobo 2012-07-11 18:54:15
我的代码只是显示如何加载和使用任何配置文件。 – 2012-07-11 18:59:00
是不是可以使用配置文件? – chobo 2012-07-11 18:31:38