Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 368
  • Last Modified:

VB 2005 How to change My.Settings Connection String

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?
  • 4
1 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...
Accually the single quotes are needed in queries, they should not be needed in your connection string.
alucky8ballAuthor Commented:
I'm sorry, but what does the '{0}' refer to?
- 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...
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.


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now