Connection String in app.config of a dll

后端 未结 2 1844
粉色の甜心
粉色の甜心 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:

    
      
        
          MyValue
        
      
    
    

    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...

    
        
              

    ... 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.

提交回复
热议问题