none
How to code link tme connectionstring from App. config RRS feed

  • Question

  • In the past I hand coded the connectionString but I read somewhere that it is a bad

    practice. Now I am trying call it from App.config but I am not having much luck.

    Will appreciate if I can get some help

    <?xmlversion="1.0"encoding="utf-8"?>

    <configuration>

    <configSections>

    </configSections>

    <connectionStrings>

    <addname="SecSchool.My.MySettings.SecDbConnectionString"

    connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=&quot;C:\VB.Net Secondary\Secdb.accdb&quot;"

    providerName="System.Data.OleDb"/>

    </connectionStrings>

    <startup>

    <supportedRuntimeversion="v4.0"sku=".NETFramework,Version=v4.5"/>

    </startup>

    </configuration>


    Wednesday, June 14, 2017 10:39 PM

All replies

  • Add a reference to the project for System.Configuration. Add a Import statement for System.Configuration for the code below.

    ConfigurationManager.ConnectionStrings(1).ConnectionString

    You can even diset the connection string e.g.

    Dim builder As New OleDb.OleDbConnectionStringBuilder With {.ConnectionString = ConfigurationManager.ConnectionStrings(1).ConnectionString}
    Console.WriteLine(builder.DataSource)
    Console.WriteLine(builder.ConnectionString)

    Use it

    Using cn As New OleDbConnection With {.ConnectionString = ConfigurationManager.ConnectionStrings(1).ConnectionString}
        cn.Open()
    End Using


    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

    Wednesday, June 14, 2017 11:08 PM
    Moderator
  • Hi alobi,

    In addition to Kareninstructor's opinion, more detailed info about getting connectionstring from app.config, please refer to:

    https://www.aspsnippets.com/Articles/Read-Get-Connection-String-from-AppConfig-file-using-C-and-VBNet.aspx

    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.

    Thursday, June 15, 2017 1:46 AM
    Moderator
  • Thanks immensely. I Will apply now and see how it goes 
    Thursday, June 15, 2017 7:53 AM
  • Be aware that bad practise shows often bad knowledge of somebody who uses it. 

    If you always turn left you are sure you come back on the same place as you started, but that does not make it good practise. 

    However, your connectionstring in the settings can be used with

        Dim con As New OleDb.OleDbConnection(My.Settings.SecDbConnectionString)


    Success
    Cor

    Thursday, June 15, 2017 8:50 AM
  • Thanks
    Thursday, June 15, 2017 11:25 AM
  • Thanks Cor
    Thursday, June 15, 2017 11:26 AM
  • Hi alobi,

    Do you resolve the issue, if so, please mark the helpful reply as answer, it will be benificial to other communities who have the similar issue.

    If not, please feel free let us know.

    Best regards,

    Cole Wu


    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.

    Tuesday, June 27, 2017 5:22 AM
    Moderator