【问题标题】:Set connectionstring in data layer class library using the app.config of a consuming assembly使用消费程序集的 app.config 在数据层类库中设置连接字符串
【发布时间】:2016-06-25 12:13:00
【问题描述】:

我有一个带有数据集(.xsd 文件)的类库项目

DataSet 默认将其连接字符串存储在 app.config 文件中。

我还有一个控制台应用和一个 winforms 应用引用这个类库。

是否可以将类库使用的连接字符串设置以某种方式放在我的消费程序集的 app.config 中?

我了解使用configSections 的其他设置是可能的,如此处接受的答案中所述: What use has the default (assembly).dll.config file for .NET-Assemblies?

但这似乎不适用于连接字符串,因为它们没有存储在配置部分中。

我尝试将类库 app.config 中的 <add .. 标记复制到消费程序集 app.config 的 <connectionStrings> 部分 - 无效。

我的类库的app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="MyDatabaseLib.Properties.Settings.myConnectionString"
            connectionString="Data Source=mydbserver;Initial Catalog=mydb;Persist Security Info=True;User ID=myuser;Password=mypass"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

【问题讨论】:

    标签: .net dll app-config


    【解决方案1】:

    从类库中删除 app.config 并为控制台应用程序和 Windows 应用程序添加 app.config 并在类库中用于读取连接字符串

    System.Configuration.ConfigurationManager.ConnectionStrings["connectionstrnigname"].ConnectionString;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2012-01-18
      • 2013-04-15
      相关资源
      最近更新 更多