insert variables into SQL connection string

Hi

I'm trying to create a VB connection string using variables but I think I'm getting the syntax all wrong, see below.

I have 4 variables which I'm trying to get into the string but I just cant get this to work,

Any suggestions?

Thanks
Garry Noble
Dim username As String
        Dim password As String
        Dim database As String
        Dim server As String
 
        username = txtUsername.Text
        password = txtPassword.Text
        database = txtDatabase.Text
        server = txtServer.Text
 
        Dim connection As String = "server="server"; database="database"; uid="username"; pwd="password""

Open in new window

garrynoblekAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LimbeckCommented:
hi, try it like this:

        Dim connection As String = "server=" & server & "; database=" & database & "; uid=" & username & "; pwd=" & password
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
YiogiCommented:
& like limbeck suggested or + signs, like below will do it.

Dim connection As String = "server=" + server +  "; database=" + database + "; uid=" + username +  "; pwd=" + password

You are after all trying to do string concatenation so you need that extra symbol to tell the program what to do.

or you can try string.Format if you want like:
Dim connection As String
connection = string.Format("server={0}; database={1};uid={2};pwd={3}", server, database, username, password)
0
garrynoblekAuthor Commented:
Thanks
Garry Noble
0
LimbeckCommented:
glad to be of help.

Ed.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.