[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3289
  • Last Modified:

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

0
garrynoblek
Asked:
garrynoblek
  • 2
1 Solution
 
LimbeckCommented:
hi, try it like this:

        Dim connection As String = "server=" & server & "; database=" & database & "; uid=" & username & "; pwd=" & password
0
 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

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