我该如何将
这种方式不起作用,因为它只返回一个
我该如何做到这一点?
SqlDataReader
返回的值添加到泛型列表中?我有一个方法使用 SqlDataReader
从 Category
表中获取 CategoryID
。我想将所有CategoryID
添加到一个泛型列表中。这种方式不起作用,因为它只返回一个
categoryID
,即最后一个。 我想将所有的categoryID
添加到列表中,然后返回它们。我该如何做到这一点?
SqlConnection connection = null;
SqlDataReader reader = null;
SqlCommand cmd = null;
try
{
connection = new SqlConnection(connectionString);
cmd = new SqlCommand("select CategoryID from Categories", connection );
connection.Open();
List<int> catID = new List<int>();
dr = cmd.ExecuteReader();
while (dr.Read())
{
catID.Add(Convert.ToInt32(dr["CategoryID"].ToString()));
}
}
finally
{
if (connection != null)
connection.Close();
}
return catID;
catId
的 字段 吗? - SLaks