Insert variables for Server info

G

Guest

Can anyone tell me how to insert variables for server info in a SQL
connection statement.

For example, in the following statement:
("Server=KIN-SSQL02;" & "Database=FTQ;" & "User ID=abc;" & "Password=xyz;")

I tried using the following variables, but it doesn't work.
'("Server='varServer';" & "Database='varDatabase';" & "User
ID='varUserName';" & "Password='varPassword';")

In debug mode, I can hover over the variables and it shows the correct
information but I still get an error.

Thanks in advance,

Mike
 
J

Jack Jackson

Can anyone tell me how to insert variables for server info in a SQL
connection statement.

For example, in the following statement:
("Server=KIN-SSQL02;" & "Database=FTQ;" & "User ID=abc;" & "Password=xyz;")

I tried using the following variables, but it doesn't work.
'("Server='varServer';" & "Database='varDatabase';" & "User
ID='varUserName';" & "Password='varPassword';")

In debug mode, I can hover over the variables and it shows the correct
information but I still get an error.

Anything in a text string is just text. There is no way to embed a
variable name in a text string and have the variable name evaluated.

I think the clearest way is to use String.Format:

String.Format("Server={0};Database={1};User ID={2};Password={3}", _
New Object() {varServer, varDatabase, varUserName, varPassword})

You can also use StringBuilder, or just concatenate strings:
"Server=" + varServer + ";Database=" + varDatabase ...
 
R

rowe_newsgroups

Can anyone tell me how to insert variables for server info in a SQL
connection statement.

For example, in the following statement:
("Server=KIN-SSQL02;" & "Database=FTQ;" & "User ID=abc;" & "Password=xyz;")

I tried using the following variables, but it doesn't work.
'("Server='varServer';" & "Database='varDatabase';" & "User
ID='varUserName';" & "Password='varPassword';")

In debug mode, I can hover over the variables and it shows the correct
information but I still get an error.

Thanks in advance,

Mike

I suspect your trouble is in your string concatenation. Try:

Dim connectString As String =
String.Format("Server={0};Database={1};User ID={2};Password={3};",
varServer, varDatabase, varUserName, varPassword)

Thanks,

Seth Rowe
 
G

Guest

Actually that didn't work but I made a couple of changes that did work.

("Server=" & varServer & ";" & "Database=" & varDatabase & ";" & "User
ID=" & varUserName & ";" & "Password=" & varPassword & ";")

This way I can get the values from the registry and not "hard code" it in
the program.

Thanks for your input.

Mike
 
J

Jack Jackson

I suspect your trouble is in your string concatenation. Try:

Dim connectString As String =
String.Format("Server={0};Database={1};User ID={2};Password={3};",
varServer, varDatabase, varUserName, varPassword)

String.Format takes a maximum of 3 separate replacement arguments. For
more you have to pass an array.
 
R

rowe_newsgroups

String.Format takes a maximum of 3 separate replacement arguments. For
more you have to pass an array.

Umm... not quite. It takes 3 separate named arguments before you pass
the values as a ParamArray. Which acts just the same as the "normal"
String.Format with 3 or less arguments.

For example:

////////////////////
Module Module1

Sub Main()

Dim s As String = String.Format("{0} {1} {2} {3} {4} {5}",
"one", "two", "three", "four", "five", "six")
Console.WriteLine(s)

Console.Read()

End Sub

End Module
///////////////////

That passes six arguments in the exact same format as if it were used
with 3 or less arguments - there is no need to declare a separate
array of strings and pass it in.

Thanks,

Seth Rowe
 
J

Jack Jackson

Umm... not quite. It takes 3 separate named arguments before you pass
the values as a ParamArray. Which acts just the same as the "normal"
String.Format with 3 or less arguments.

For example:

////////////////////
Module Module1

Sub Main()

Dim s As String = String.Format("{0} {1} {2} {3} {4} {5}",
"one", "two", "three", "four", "five", "six")
Console.WriteLine(s)

Console.Read()

End Sub

End Module
///////////////////

That passes six arguments in the exact same format as if it were used
with 3 or less arguments - there is no need to declare a separate
array of strings and pass it in.

I see that the code you posted works, but I don't understand why. Can
you explain? What is ParmArray? I Googled for it and didn't find any
interesting results.
 
R

rowe_newsgroups

I see that the code you posted works, but I don't understand why. Can
you explain? What is ParmArray? I Googled for it and didn't find any
interesting results.

A ParamArray (Parameter Array) just signifies that the method can take
any number of parameters. It can only be used once per method and only
as the last variable in the method.

Here's the MSDN documentation:

http://msdn2.microsoft.com/en-us/library/ct363x9h(VS.80).aspx

Thanks,

Seth Rowe
 
J

Jack Jackson

Thank you very much. That is very helpful.

Any idea why String.Format has three overloads for one, two and three
replacement arguments? It doesn't seem like those serve any real
purpose with the ParamArray overload.
 
R

rowe_newsgroups

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top