VB 2005 How to change My.Settings Connection String

Posted on 2009-02-11
Last Modified: 2013-11-25
I have searched and found several threads on changing the connection string values in VB 2005, but have yet to find an easy way to do it.  Also, as an alternative I tried writing the connection string into an XML file, but the string would never let me connect.  Has anyone had any luck with storing a connection string in a manner that it can be easily changed in the event the datasource changes?
Question by:alucky8ball
    LVL 9

    Accepted Solution

    A simple solution might be to write your own string and in each statement your use String.Format method to check for the proper string to place into your connection string. i.e.

    Dim con As String = String.Format(" your path to your server... '{0}'... ", txtdatabaseSource)
    Make a form or place to change this path and use a textbox to enter this location, this would enter the text you typed into the textbox and place into the connection string, you will need to follow this format '{0}' the single quotes are needed to make this work.

    I have used similar ideas for queries.

    Hope this helps...

    Disclaimer, not an expert yet...
    LVL 9

    Expert Comment

    Accually the single quotes are needed in queries, they should not be needed in your connection string.

    Author Comment

    I'm sorry, but what does the '{0}' refer to?
    LVL 9

    Expert Comment

    - Whatever string is in your textbox, or any other place you can draw the string from.
    If textbox1.text is "something" then when you write:
     Dim con As String = String.Format(" your path to your server... '{0}'... ", txtdatabaseSource)' it will be
                                                           " your path to your server... something ..."
    It just adds the string inside of your connection string. I know this may seem weird(and it might be) but this will work as long as the info is right. After all it is just a string, and this is a simple manipulation of that string.
    {0} is the first value, {1} is the second value, etc... and you can reuse the {X} as many times as you need.
    " The {0} day is always the {1}!", txt1.text, txt2.text) = "The first day is always the hardest" where
    txt1.text = "first" and txt2.text = "hardest"
    These could also be comboBox1.selected.item.tostring just as well!

    Happy coding...
    LVL 9

    Expert Comment

    For clarity sake line 3(above) should have looked like
    Dim con As String = String.Format(" your path to your server... {0}... ", textbox1.text)' it will be...

    Also I have not tested this for your purposes, hope this is something you can do easily enough, and to reiderate you will not need the single quotes around the braces {}, unless you use this method in a sql statement.

    Dim sql as string = Format("SELECT * FROM myTable WHERE column1 = '{0}' OR '{1}' ", txt1.text, txt2.text)

    Makes an interface for custom searching.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
    A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
    Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
    This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now