.net 实现 一二级分类

 public List<Model.Category> CategoryPid(int id = 0)
       {
           string sql = "select * from category order by cate_sort asc ";  //顶级分类

           List<Model.Category> list = new List<Model.Category>();
           DataTable sql_id = SqlHelper.GetTableText(sql, null)[0];
           Model.Category cate = null;
           foreach (DataRow row in sql_id.Rows)
           {


          

               //if(Convert.ToInt32(row["cate_pid"])==0){
               //     cate = new Model.Category();
               //     cate.Cate_Name = row["Cate_Name"].ToString();
               //     cate.Cate_Pid = Convert.ToInt32(row["cate_pid"]);
               //     cate.Cate_Sort = Convert.ToInt32(row["Cate_Sort"]);
               //     cate.Cate_State = Convert.ToInt32(row["Cate_State"].ToString());
               //     list.Add(cate);

               // string sql_pid = "select * from category  where cate_pid=" + Convert.ToInt32(row["id"]) + " order by cate_sort asc";
               // DataTable sql_pids = SqlHelper.GetTableText(sql_pid, null)[0];
               // foreach (DataRow rows in sql_pids.Rows) 
               // {

               //     cate = new Model.Category();
               //     cate.Cate_Name = rows["Cate_Name"].ToString();
               //     cate.Cate_Pid = Convert.ToInt32(rows["cate_pid"]);
               //     cate.Cate_Sort = Convert.ToInt32(rows["Cate_Sort"]);
               //     cate.Cate_State = Convert.ToInt32(rows["Cate_State"].ToString());
               //     list.Add(cate);
               // }
               //}

           }

           return list;


       }

  

 public List<Model.Category> CategoryPid(int id = 0)
       {
           string sql = "select * from category order by cate_sort asc ";  //顶级分类

           List<Model.Category> list = new List<Model.Category>();
           DataTable sql_id = SqlHelper.GetTableText(sql, null)[0];
           Model.Category cate = null;
           foreach (DataRow row in sql_id.Rows)
           {


               if (Convert.ToInt32(row["cate_pid"]) == id)
               {
                   cate = new Model.Category();
                   cate.Cate_Name = row["Cate_Name"].ToString();
                   cate.Cate_Pid = Convert.ToInt32(row["cate_pid"]);
                   cate.Cate_Sort = Convert.ToInt32(row["Cate_Sort"]);
                   cate.Cate_State = Convert.ToInt32(row["Cate_State"].ToString());
                
                   list.Add(cate);
                   list.AddRange(CategoryPid(Convert.ToInt32(row["id"])));

               }



            

           }

           return list;


       }

 

posted @ 2017-06-01 17:43  尘梦  阅读(314)  评论(0编辑  收藏  举报