Explain .ConnectionString Mechanism

I am interested to know how the ConnectionString line of code works.  It appears to search machine.config, app.config and web.config.  Anybody have any background on how this process works   Does it really search all three files and in what order



Protected Function DB(ByVal DatabaseName As String) As String

Return ConfigurationManager.ConnectionStrings _
(DatabaseName).ConnectionString

 End Function

 



Answer this question

Explain .ConnectionString Mechanism

  • Renaud Martinon

    Are there any genuises at MS that have any background information on this I would love to see a KB article on this subject.

    Somebody should consider authoring one if it doesn't exist.


  • guenthk

    Let me be a little more clear...

    I meant to say that a DLL can't have an app.config. I couldn't read from app.config when I try to add it to my class library (DLL). You can however add an app.config file to WindowsApp that references your DLL and the DLL will be able to read from that.

    I think I would disagree with you that it reads machine.config then app.config / web.config in that order. I would think that it looks for the more specific local file first and then goes to the more general machine.config. I believe the reason web.config works on the web server is because the app on that server references the DLL so the DLL is able to find web.config.


  • Juan Escalante

    It's been weeks since I read the orginal article and I didn't save the link. I found this KB article in a quick search on the subject, but I'm not sure if this is the original article I read

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/vbcon/html/vboriintroductiontoapplicationsettingstorage.asp


  • Dan Kershaw

    In the app.cfg write the following code

    < xml version="1.0" encoding="utf-8" >

    <configuration>

    <appSettings>

    <add key="cstrg" value="packet size=4096;data source=&quot;.&quot;;persist security info=True;initial catalog=KWDR;" />

    </appSettings>

    </configuration>

    In the vb file write the following code

    Dim config As New System.Configuration.AppSettingsReader

    Dim ID As String = "user id=" & Me.txtUserID.Text & ";password=" & Me.txtPassword.Text & ""

    Dim cstrg As String = config.GetValue("cstrg", GetType(System.String))

    ConnectionString = cstrg & ID



  • SteveBouffard

    plankton wrote:
    It appears to search machine.config, app.config and web.config.

    How did you figure it out Would like to shed some light on that .



  • Eric Shan

    In your web.config/app.config:

    <configuration>

    <connectionStrings>

    <add name="SomeKey" providerName="System.Data.SqlClient" connectionString="server=machine; database=db; uid=user; pwd=password"/>

    </connectionStrings>

    In your code:

    String Conn = System.Configuration.ConfigurationManager.ConnectionStrings["SomeKey"].ConnectionString;

    I haven't played with/investigated the providerName yet.

    If you change the .ConnectionStrings to .AppSettings and you can read the app settings section of your .config file.

    Does this help


  • Rich Wood

    I originally added an app.config file to my assembly, but I couldn't ever get it to read from the file. It appears that an assembly can't have an app.config so it must look elsewhere for the string.

    After investigation, it figured out it was pulling the value from machine.config. I deleted the app.config file from my assembly and recreated it as part of an .exe in the same project. The code then began to work correctly pulling the value from app.config and not from machine.config. When I move the assembly to a web server its pulls the string from web.config. It appears to pull values from all three types of files, but I have yet to find any documentation on how the internals work.

    Anybody got documentation on this


  • noctifer

    Anybody at Microsoft care to enlighten us by responding to the original post
  • Ira Berkowitz

    Hi!

    Thats great you found the information, can you post the link so that others can benefit.

    thanks



  • JoDask

    I'm using the code that's in my original post.

    I couldn't get it to work when I added app.config to the DLL so I did some searching and found a KB article which says it won't work with a DLL. It said that you could get it to read from an EXE's app.config if that EXE references your DLL. I was able to verify that this is indeed the case.

    I am a litle puzzled that the IDE let you add an app.config file to a class library and it also creates [dll name].dll.config. Seems goofy that they would create the file files for a DLL if they could never be used. Maybe there is a different set of code that will work. My original issue is that documentation sucks on this subject.


  • jhowa1

    Hi!

    In Dot Net terms both EXE and DLL are assemblies.

    Web.config and App.config both are same except Web.config is used for Web Applications and App.config is used for Windows Applications.

    Logically speaking you should be bothered about Web and App config files only because they are directly related to your applications. Whereas Machine.config is something global to all of your applications irrespective of web or windows. Hence Machine config is not the file to look for database path that you want to use for your application.

    Sequence is  App/Web.config and then Maching.config.

    And as you mentioned assembly can't have app.config is wrong. May be you are doing something wrong thats why you are not able to access it. Otherwise each and every assembly can have and access app.config file whether EXE or DLL it doesn't matter.

    For details you can check MSDN on these files.

    cheers.



  • eayrider

    I originally added an app.config file to my class library project, but I couldn't read from it. I researched the problem and found an article which seemed to suggest you can't use an app.config file with a DLL, but that it was possible to read an EXE's app.config file if it referenced the DLL. I tried this approach and it worked, but I was always curious why the IDE would let you create an app.config file as part of a DLL project if it wouldn't let you use it and why it created [dll name].dll.config in the debug and release folders. I thought this was the end of the story, but it was not.

    I was recently trying to debug some issues related to some TableAdapters and noticed that the TableAdapter wizard created an app.config file as part of my class library. I thought this was goofy place to put the file since the DLL couldn't make use of it. After some testing I noticed that the wizard edited the file just fine in that location so I took another crack at reading the file from that location. Now for some reason I can read from the app.config file. I am baffled at this point as to how these damn config files work


  • shusen

    Hi!

    How you are reading from app.config when using it with DLL . Can you write your code here. Coz not only you add config file to DLL but you can also read it.

    For sequence you are right but it matters only when you are doing something that is related to DotNet Frame work like in case of Remoting. If your app.config doesn't contain information regarding the channel to use it will look for that in Machine.config. But in your case its database path which is specific to your application and hence should be searched in your app.cofig.

    cheeers



  • e-milio

    Plankton, one thing you can do is load an appDomain and I believe that will let you retrieve those settings. You are right that it does probe, starting at the application level and going to the machine level if configuration file aren't present. In previous versions of the framework you could specify another file for it to look to so that you could have multiple files that a config file refers to. In the current version of the framework you can use a mapping file.

    Ironically, I'm working on some content for Microsoft right now that covers this material. I can probably get a KB article up tomorrow (although it make take a few days to actually post, I'm not sure what the current turn around time is).

    Shoot me an email tomorrow or tonight WilliamRyan @ gmail dot com (my current user id goes to WilliamGRyan @ gmaildot com but I get a lot of stuff eaten by the spam filter. Anyway, there is some content that I've been working on that isn't proprietary and I'd be glad to send you a copy of the proofs so that you'll have some hopefully helpful information tomorrow until the KB article posts. If you're interested, just drop me a line and I'll try to get everything together tomorrow and shoot it over to you. I've already been working on some non-proprietary content so I can probably compile everything and have it ready by mid afternoon.

    Cheers,

    Bill



  • Explain .ConnectionString Mechanism