我在表格中有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();
由于
答案 0 :(得分: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));
}
}
}
未经测试!
答案 1 :(得分:0)
更正代码:
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();
请注意,在我解决您的直接问题时,您还会遇到很多其他问题,例如您的rowsinmachgrp
变量,使用ArrayList
,而不是使用{{ 1}}:)