开发者

put names in table into arraylist?

i have 5 names in a table and i need to put these in an arraylist....

any suggestions???

            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 na开发者_开发技巧mes = new ArrayList();
                dataConnection.Open();
                ArrayList names = dataCommand.ExecuteScalar();

Thanks


This is yours:

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

Not Tested!


Corrected Code:

ArrayList names = new 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);


  dataConnection.Open();
  SqlDataReader rdr = dataCommand.ExecuteReader();
  while (rdr.Read())
  {
    names.Add(rdr.GetString(0));
  }
dataCommand.Dispose();
dataConnection.Dispose();

Please note that while I'm solving your direct problem, you've got a lot of other issues going on, such as your rowsinmachgrp variable, using an ArrayList, and not using using :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜