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