如何读取sql server中表的列中的值?

时间:2009-03-24 13:46:09

标签: sql-server

有一个名为Friends的表。它有名为Names,Address,Phone no,Dob的列。我们想逐个使用Friends表中的Names。所以我想将所有名称存储在一个稍后要使用的数组中。

3 个答案:

答案 0 :(得分:1)

这是纲要:

在你的.config文件中:

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

在您的代码中:

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

答案 1 :(得分:0)

以下是Google搜索的最高结果

 get sql data c#

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

您想要的SQL语句是

 select * from friends

答案 2 :(得分:0)

SELECT Name FROM Friends ORDER BY Name