.net 将连接字符串传递到dll?

.net 将连接字符串传递到dll?,.net,dll,shared-libraries,app-config,.net,Dll,Shared Libraries,App Config,我正在尝试创建一些需要数据库访问的简单库。我不知道的是如何将连接字符串传递到库。。。现在我的dll中有一个db.config,但我不确定如何从dll中引用它等等 这就是我设置库的方式 [解决方案文件] 图书馆1 db.config 图书馆2 链接的db.config 如何从dll中引用db.config 如何从web应用程序web.config引用db.config 非常简单: 1) 如果这恰好是一个.Net.dll,您可以将其存储在“app.config”或“web.con

我正在尝试创建一些需要数据库访问的简单库。我不知道的是如何将连接字符串传递到库。。。现在我的dll中有一个db.config,但我不确定如何从dll中引用它等等

这就是我设置库的方式

[解决方案文件]

  • 图书馆1
    • db.config
  • 图书馆2
    • 链接的db.config


  • 如何从dll中引用db.config
  • 如何从web应用程序web.config引用db.config
  • 非常简单:

    1) 如果这恰好是一个.Net.dll,您可以将其存储在“app.config”或“web.config”中:

    :

    
    字符串cnString=
    ConfigurationManager.ConnectionString[“Digibrad”]。ConnectionString;
    
    2) 您还可以将连接字符串存储在.dll可以读取的任何位置。例如,您可以使用.ini文件或注册表

    3) 您可以在.dll中实现
    getConnectionString()
    方法


    4) 等等等等等等回答你的第一个问题:

    我通常更喜欢使用以下ConfigurationManager方法之一:

    或者有一种带有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"));
    
    XDocument document=XDocument.Load(configFileFullName);
    XElement configurationElement=document.Element(“配置”);
    XElement appSettingsElement=configurationElement.Element(“appSettings”);
    列表配置设置=新列表(appSettingsElement.substands(“add”);
    
    不能使用配置文件吗?是库中引用的配置文件还是调用库的应用程序?现在,我的应用程序中有一个constings.config文件,在类库中,我使用字符串connString=ConfigurationManager.connectionString[“DBConnectMain”].ToString()调用它;但这似乎不是一个好主意。我的代码只是展示了如何加载和使用任何配置文件。
    XmlDocument webConfig = new XmlDocument();
    webConfig.Load(dllConfigFileName);
    XmlNode someNode = webConfig.SelectSingleNode("//configuration/appSettings/add[@key='someKey']");
    
    XDocument document = XDocument.Load(configFileFullName);
    XElement configurationElement = document.Element("configuration");
    XElement appSettingsElement = configurationElement.Element("appSettings");
    List<XElement> configSettings = new List<XElement>(appSettingsElement.Descendants("add"));