tags:

views:

55

answers:

3

There is a table called Friends. It has columns named Names, Address, Phone no, Dob. We want to use Names which are in Friends table one by one. so I want to store all names in an array which is to be used later.

A: 

Here is the top result from googling

 get sql data c#

http://www.devhood.com/Tutorials/tutorial_details.aspx?tutorial_id=454

The SQL statement you want is

 select * from friends
Lou Franco
A: 
SELECT Name FROM Friends ORDER BY Name
Josh Stodola
+1  A: 

Here's the rundown:

In your .config file:

<configuration>
  <connectionStrings>
    <add name="Test" connectionString="Data Source=[server name here];Initial Catalog=[db name here];User Name=blah;Password=blah"/>
  </connectionStrings>
</configuration>

In your code:

using System.Configuration;
using System.Data.SqlClient;

...

// In ASP.NET use WebConfigurationManager instead...
string connectionString = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand command = new SqlCommand("SELECT Name FROM Friends ORDER BY Name", connection);

List<string> nameList = new List<string>();
using (SqlDataReader reader = command.ExecuteReader())
{
    while (reader.Read())
    {
        nameList.Add(reader["Name"] as string);
    }
}
string[] names = nameList.ToArray();
Tor Haugen