1.B,源代码(主要代码摘要) /App_Code/DBConnection.cs /App_Code/CategoryInfo.cs 代码如下: using System.Collections.Generic; public class CategoryInfo { int categoryid; string categoryname; string categorydesc; IList<ArticleInfo> articles; /// <summary> /// 1,子嵌套数据 /// </summary> public IList<ArticleInfo> Articles { get { return articles; } set { articles = value; } } public int Categoryid { get { return categoryid; } set { categoryid = value; } } public string Categoryname { get { return categoryname; } set { categoryname = value; } } public string Categorydesc { get { return categorydesc; } set { categorydesc = value; } } public CategoryInfo() { } public CategoryInfo(int categoryid, string categoryname, string categorydesc,IList<ArticleInfo> articles) { this.categoryid = categoryid; this.categoryname = categoryname; this.categorydesc = categorydesc; this.articles = articles; } }
/App_Code/ArticleInfo.cs /App_Code/CategoryOper.cs 代码如下: using System.Data; using System.Data.SqlClient; using System.Collections.Generic; public class CategoryOper { public static IList<CategoryInfo> SelectAll() { IList<CategoryInfo> allcate = new List<CategoryInfo>(); string sql = "select category.categoryid,categoryname,categorydesc,id,title,author from category inner join article on category.categoryid=article.categoryid order by category.categoryid"; SqlConnection con = new DBConnection().Con; SqlCommand com = new SqlCommand(); com.Connection = con; com.CommandText = sql; com.CommandType = CommandType.Text; con.Open(); SqlDataReader sdr = com.ExecuteReader(); int tempcategoryid=0; CategoryInfo cate=null; while (sdr.Read()) { int categoryid=sdr.GetInt32(0); //如果类别改变则创建一个新的 cate 对象 if(categoryid!=tempcategoryid) { cate = new CategoryInfo(sdr.GetInt32(0), sdr.GetString(1), sdr.GetString(2), new List<ArticleInfo>()); allcate.Add(cate); tempcategoryid = categoryid; //把新类别编号付给标识 } ArticleInfo art = new ArticleInfo(sdr.GetInt32(3), sdr.GetString(4), sdr.GetString(5)); cate.Articles.Add(art); } con.Close(); return allcate; } public CategoryOper() { // // TODO: 在此处添加构造函数逻辑 // } }