c # - Rellene la lista genérica de SqlDataReader
¿Cómo puedo agregar valores que unSqlDataReader
vuelve a una lista genérica? Tengo un método donde usoSqlDataReader
LlegarCategoryID
a partir de unaCategory
mesa. Me gustaría agregar todas lasCategoryID
una lista genérica.
sta dosis no funciona porque solo devuelve unacategoryID
y ese es el último. Quiero agregar todas lascategoryID
a la lista y luego devolverlos.
¿Cómo puedo hacer eso
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;