7 Replies - 559 Views - Last Post: 14 September 2011 - 02:49 PM Rate Topic: -----

#1 gordonheimer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 24-April 09

SQL connection

Posted 14 September 2011 - 10:48 AM

I have an array of databases that I need to loop through and pull information. I cannot figure out how to properly declare the connection statement by using the array.

 SqlConnection conect = new SqlConnection("user id=admin password=pass server=localhost database=array{0}");


I tried doing {0},array[0] but I am not sure how to declare this statement.

Thanks!
Is This A Good Question/Topic? 0
  • +

Replies To: SQL connection

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8382
  • View blog
  • Posts: 31,155
  • Joined: 12-June 08

Re: SQL connection

Posted 14 September 2011 - 10:55 AM

Is your database called array or the array holds database names?


The former is just odd, the latter is: stop making a literal string!

SqlConnection conect = new SqlConnection("user id=admin password=pass server=localhost database=" + array[0].tostring());

Was This Post Helpful? 1
  • +
  • -

#3 gordonheimer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 24-April 09

Re: SQL connection

Posted 14 September 2011 - 10:57 AM

I have an array that holds databases names, I wasnt sure how else to store them.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,366
  • Joined: 02-June 10

Re: SQL connection

Posted 14 September 2011 - 11:06 AM

Array elements are accessed with brackets [], not braces {}
Arrays are zero-indexed, meaning the first element is number 0:
myArray[0] is the first element
myArray[1] is the second element
and so on.
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: SQL connection

Posted 14 September 2011 - 01:26 PM

1. Your connection string is in the wrong format. Between each "section", you need a semicolon. You have no semicolons.

2. You need to loop through the array(using a for or foreach loop) and for each database name, you will be creating a connection and other objects.

3. You will want to use the value from the array element, not the literal string "array[0]".
Was This Post Helpful? 0
  • +
  • -

#6 sithius92  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 160
  • Joined: 01-August 08

Re: SQL connection

Posted 14 September 2011 - 01:31 PM

When I had to make a connection, I had to put semi-colons between each property of the connection string. Could that be the case here?

Edit: eclipsed4utoo got to it first

This post has been edited by sithius92: 14 September 2011 - 01:31 PM

Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,366
  • Joined: 02-June 10

Re: SQL connection

Posted 14 September 2011 - 02:47 PM

Why don't you try it rather than sit around waiting for one of us to do it for you?

Can I make a suggestion, even though you won't like it?
You don't seem familiar with making connections.
You don't seem familiar with accessing array elements.
You don't seem to be able to determine that values within quotes aren't going to be values that get updated.

Perhaps you're over reaching. Perhaps you should concentrate on learning and following a book and tutorials. Just work on exercises from learning resources for now.

Don't try to create a useful working program to fit a need of yours (or a for-pay contract) as your introduction to coding project. When you are learning to code you don't know enough to code a program, let alone know how to engineer the architecture of a program. It would be like saying "I don't know how to read sheet music, or play an instrument. I think I'll write a 3 act opera as my first learning experience."

I don't say this to be mean. We've seen lots of new coders take this approach and we know it doesn't work. Trying to design your own programs before you understand the basics of the code language you've chosen just leads to problems, frustrations, and 'swiss-cheese' education (lots of holes).


Resources, references and suggestions for new programmers. - Updated Sep 2011
Spoiler

Was This Post Helpful? 1
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: SQL connection

Posted 14 September 2011 - 02:49 PM

Or use the inbuilt data source connection wizard and dataset designer.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1