473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Insert variables for Server info

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

Oct 1 '07 #1
9 1192
On Mon, 1 Oct 2007 10:11:06 -0700, MikeS
<Mi***@discussions.microsoft.comwrote:
>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 ...
Oct 1 '07 #2
On Oct 1, 1:11 pm, MikeS <Mi...@discussions.microsoft.comwrote:
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

Oct 1 '07 #3
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

"rowe_newsgroups" wrote:
On Oct 1, 1:11 pm, MikeS <Mi...@discussions.microsoft.comwrote:
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

Oct 1 '07 #4
On Mon, 01 Oct 2007 11:11:22 -0700, rowe_newsgroups
<ro********@yahoo.comwrote:
>On Oct 1, 1:11 pm, MikeS <Mi...@discussions.microsoft.comwrote:
>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)
String.Format takes a maximum of 3 separate replacement arguments. For
more you have to pass an array.
Oct 2 '07 #5
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
Oct 2 '07 #6
On Tue, 02 Oct 2007 11:35:33 -0700, rowe_newsgroups
<ro********@yahoo.comwrote:
>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.
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.
Oct 3 '07 #7
On Wed, 03 Oct 2007 03:25:39 -0700, rowe_newsgroups
<ro********@yahoo.comwrote:
>http://msdn2.microsoft.com/en-us/lib...9h(VS.80).aspx
Thank you very much. That is very helpful.
Oct 3 '07 #8
On Wed, 03 Oct 2007 09:46:51 -0700, Jack Jackson
<ja********@pebbleridge.comwrote:
>On Wed, 03 Oct 2007 03:25:39 -0700, rowe_newsgroups
<ro********@yahoo.comwrote:
>>http://msdn2.microsoft.com/en-us/lib...9h(VS.80).aspx

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.
Oct 4 '07 #9
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.
Fortunately for me, I recalled seeing a question similar to this in
the C# newsgroup:

http://groups.google.com/group/micro...0e63e96f8a089f

Basically, it's either for readability or compatibility with
other .Net languages.

Thanks,

Seth Rowe
Oct 4 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: newbie_mw | last post by:
Hi, I need urgent help with a novice problem. I would appreciate any advice, suggestions... Thanks a lot in advance! Here it is: I created a sign-up sheet (reg.html) where people fill in their...
7
by: Luis | last post by:
Application X has three screens. The user captures information on screen one and then clicks a navigation button to go onto screen two. He does the same on screen three. At the bottom of screen...
1
by: PT | last post by:
I got a problem. And thats..... First of all, I got these three tables. ------------------- ------------------ ---------------------- tblPerson tblPersonSoftware ...
14
by: Paul Yanzick | last post by:
Hello, I am trying to develop a book tracking application for my capstone in school, and am running into a problem. The application is an ASP.Net application written in C#. The first page you...
4
by: G.Esmeijer | last post by:
Friends When I use a Querystring for insertdating data into a Tabel (going to SQLserver) the decimal pint changes to a comma. The result is that I get a system error (which I can understand) ...
3
by: | last post by:
I'm picking up an 'IMPORTS' error for a simple database insert based on two input entry boxes in my form? It says an 'Imports' statement must preceede any declarations....... is this perahps the...
7
by: | last post by:
I am having trouble figuring out to call a database INSERT procedure from a simple submit form. It appears I should use the onclick event to trigger the procedure called BUT when I do this I...
2
by: kisalabs | last post by:
I have been trying to insert a new record into an access database and cannot get any of the scripts to work.. I can access the db and pull up/display records but cannot get one added. Please help. ...
1
by: Maklar60 | last post by:
I am attempting to execute an INSERT statement on my page but continually get the following error: Microsoft OLE DB Provider for ODBC Drivers error '80040e14' Incorrect syntax near '<'. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.