siva sangaran
siva sangaran

Reputation:

how to read values in the column of a table in sqlserver?

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.

Upvotes: 0

Views: 320

Answers (3)

Tor Haugen
Tor Haugen

Reputation: 19627

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();

Upvotes: 1

Josh Stodola
Josh Stodola

Reputation: 82483

SELECT Name FROM Friends ORDER BY Name

Upvotes: 0

Lou Franco
Lou Franco

Reputation: 89172

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

Upvotes: 0

Related Questions