0
我有这样的方法:返回类别对象的名单,我要将此列表输出在C#中下拉列表中的数据源,我怎么能做到这一点集列表
public List<Category> GetAllCategories()
{
SqlConnection con = new SqlConnection(connectonstring);
SqlCommand cmd = new SqlCommand("GetAllCategories", con);
cmd.CommandType = CommandType.StoredProcedure;
List<Category> Categories = new List<Category>();
try
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Category cat = new Category();
cat.JobCategoryid = Convert.ToInt32(reader["JobCategoryid"]);
cat.CategoryName = reader["categoryName"].ToString();
Categories.Add(cat);
}
reader.Close();
return Categories;
}
catch (SqlException err)
{
return null;
}
finally
{
con.Close();
}
}
感谢Lcarus,所以它的简单,我试着这样做对自己复杂:列表名单=新名单; list = GetAllCategories(); .... –
Bruce