none
Configuration RRS feed

  • Question

  • Just came across this code:

    Dim config As Configuration
    config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
    Return config.ConnectionStrings.ConnectionStrings(key).ConnectionString

    If I were a compiler asked to compile that, I would self-destruct and take all your code with me!
    Whatever happened to 

    My.Settings.MyConnectionString




    Mary

    Saturday, February 10, 2018 2:02 AM

All replies

  • I believe the reason here is for accessing one of many connections in the config file while when there is only one connection we use My.Settings.SomeConnection.

    See my MSDN code sample for multiple connections in one project. Then there is sharing connections across projects as shown in my MSDN code sample.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, February 10, 2018 2:57 AM
    Moderator
  • Hi Mary,

    I agree with Karen's point.

    Let's firstly  App.config file.

    When you add Data Source, the <connectionStrings> section will be inserted into app.config file.
    When you add Settings variables (User scop and Application scop), the <userSettings> section and the <applicationSettings> will be appened to app.config file.

    Then you can use ConfigurationManager.AppSettings feature to store/retrieve settings from entire app.config file including these sections: <configSections>, <connectionStrings>, <system.diagnostics>, <userSettings> and <applicationSettings>.
    While you can use My.Settings feature to only store/retrieve settings from <connectionStrings>, <userSettings> and <applicationSettings> sections.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 12, 2018 3:18 AM
    Moderator
  • I use more than one connection string thusly...

    Private strSQLconn As String = My.Settings.CoffeeConnectionString

    Private strAconn As String = My.Settings.KeurigConnectionString

    If this is working for me, can I continue with what is, to me, the easy way.


    Mary

    Monday, February 12, 2018 11:21 PM
  • If I only want connection strings, not all that other stuff, can I continue with My.Settings?

    Mary

    Monday, February 12, 2018 11:22 PM
  • If I only want connection strings, not all that other stuff, can I continue with My.Settings?

    Mary

    Sure.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, February 13, 2018 12:36 AM
    Moderator
  • I use more than one connection string thusly...

    Private strSQLconn As String = My.Settings.CoffeeConnectionString

    Private strAconn As String = My.Settings.KeurigConnectionString

    If this is working for me, can I continue with what is, to me, the easy way.


    Mary

    If it works for you then use it. I've never used this method for developing enterprise apps, personal applications that you may change the connection string is fine yet the majority of apps built only change their connection strings when going from dev to test to prod and there are easier ways to do this using the same thing that web apps do in desktop apps by having separate config files for dev, test and prod but naively VS does not outwardly support them but can be done with VS extensions  

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, February 13, 2018 12:41 AM
    Moderator
  • Went to https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    to check out ConfigurationManager.AppSettings. The read all method told me I had nothing. I tried writing then reading and I got the one setting I added. Nothing changed in app.config but ApplicationName.exe.config in the bin\Debug folder did change. I guess I am still confused.


    Mary

    Friday, February 16, 2018 11:14 PM
  • Currently, desktop app but I like to learn to do things the right way. Actually I learned about storing the connection string form Application properties (I liked the idea that I could test the connection right there) in an MS walkthrough.

    Mary

    Friday, February 16, 2018 11:18 PM