[英]add non specific number of dataset rows into a list
I am trying to insert a non specific number of rows from a dataset into a list by using a foreach. 我试图通过使用foreach将非特定数量的行从数据集中插入到列表中。 But I am unsure how to add a non specific number of items to a list from a dataset. 但是我不确定如何从数据集中将非特定数量的项目添加到列表中。
public void DeviceReset(string r)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText =
"SELECT installation_id FROM masterinstallationmaps WHERE masterinstallation_id = '" + r + "' ";
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
I am picking out the installation_id from my masterinstallationmap table and if it contains more than 0 rows it should run the foreach to put the rows into the list, otherwise it should run a foreach only inputting 1 item to the list. 我正在从masterinstallationmap表中选择installation_id,如果它包含多于0行,则应运行foreach将该行放入列表中,否则应仅在列表中输入1个项目就运行一次foreach。
List<int> instIdList = new List<int>();
if (ds.Tables[0].Rows.Count > 0)
{
foreach (DataRow row in ds.Tables[0].Rows)
{
//How to insert all rows from the dataset?
}
}
else
{
instIdList.Add(1);
}
The else statement works fine, but nothing happens if the Dataset have more than 0 rows. else语句可以正常工作,但是如果数据集具有多于0行,则什么也不会发生。
I am unsure what to put into the foreach: 我不确定要输入的内容:
foreach (DataRow row in ds.Tables[0].Rows)
{
//How to insert all rows from the dataset?
}
Why can't you just add the value like 您为什么不可以像这样添加值
foreach (DataRow row in ds.Tables[0].Rows)
{
instIdList.Add(Convert.ToInt32(row["installation_id"]));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.