Connection String in app.config of a dll

后端 未结 2 1831
粉色の甜心
粉色の甜心 2021-01-22 21:00

I created a C# dll that uses connection string from app.config file to connect to database.

I use it in a web application (Visual Studio 2013, Windows 7) that a

相关标签:
2条回答
  • 2021-01-22 21:15

    Hardcoding the database connection in your referenced DLL's app.config file may not be the best approach. Setting the database connection string in the web.config file (or machine.config for a machine-wide setting) would allow other consumers of the DLL to use a different connection string. This would be useful if, for example, you have different Development, Quality Assurance, or User Acceptance environments with different databases that each of those deployed web applications would need.

    However, if that approach does not fit your needs, using Application Settings you can use and/or override default values from another referenced DLL. For example, if you have an assembly called Foo and within that, you used setting properties, you'd have a section of your Foo's app.config file that had something like this:

    <applicationSettings>
      <Foo.Properties.Settings>
        <setting name="MyProperty" serializeAs="String">
          <value>MyValue</value>
        </setting>
      </Foo.Properties.Settings>
    </applicationSettings>
    

    Then, within your web application, you can use this property by calling Foo.Properties.Settings.MyProperty. This approach would also allow you to set values that the Foo assembly could use. Within your web.config file, you'd include the section...

    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
              <section name="Foo.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    

    ... then add the Foo.Properties.Settings section, updating it with the values you want the Foo.DLL to use when it is included as part of your web application.

    This approach would allow you to set values that the application would use when executing code within the referenced DLL. In your case, you'd just need to put the connection string into an application setting rather than including it within the connection strings section.

    0 讨论(0)
  • 2021-01-22 21:18

    Actually the app.config file in your dll (class library project) won't affect the final host application ( in which the dll is used). So you will always need to put the proper connection settings in the host application's config file. For example, if you use the assembly/dll in winform or WPF application, then you should put the related configuration (like connection strings) in the winform or WPF app's app.config file. If the assembly/dll is used in ASP.NET web application, then we should make sure the related settings are put in the web.config file of the web application.

    0 讨论(0)
提交回复
热议问题