将表中的名字放入arraylist?

use*_*084 0 c# sql asp.net

我在表中有5个名字,我需要把它们放在一个arraylist中....

有什么建议???

            int rowsinmachgrp = getnumofrows();//gets no of rows in table

            SqlConnection dataConnection = new SqlConnection();
            dataConnection.ConnectionString = ConfigurationManager.ConnectionStrings["SumooHAgentDBConnectionString"].ConnectionString;
            SqlCommand dataCommand =
                    new SqlCommand("select MachineGroupName from MachineGroups", dataConnection);

            {ArrayList names = new ArrayList();
                dataConnection.Open();
                ArrayList names = dataCommand.ExecuteScalar();
Run Code Online (Sandbox Code Playgroud)

谢谢

Art*_*hur 5

这是你的:

List<string> names = new List<string>();
using(SqlConnection db = new SqlConnection(ConfigurationManager...))
{
    db.Open();
    SqlCommand cmd = new SqlCommand("Select ....", db);

    using(SqlDataReader rd = cmd.ExecuteReader())
    {
        while(rd.Read())
        {
           names.Add(rd.GetString(0));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

没测试过!