我有这个方法:返回类别对象的列表,我想将此列表输出设置为 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();
}
}