问题描述
用户权限问题有这样一个表MenuIDMenuNameParentMenuID1a2b12c14d25e46f5在权限表里只存了MenuID比如说张三对菜单4有操作权限,那么我就得查出和4有关的父节点以及4下边的所有子节点请高手帮忙!
解决方案
解决方案二:
sql还是C#sql用CTEC#递归
解决方案三:
递归几层啊?2是4的父,1是2的父,1取不取?5是4的子,6是5的子,6取不取如果memu表的内容不太多就读出来放到DataTable并常驻内存就行了,通过DataTable的select()函数递归
解决方案四:
递归呗。。。
解决方案五:
使用递归,对要查找的节点进行迭代!
解决方案六:
publicpartialclass_Default:System.Web.UI.Page{List<test>list;protectedvoidPage_Load(objectsender,EventArgse){list=newList<test>();GetDataTable();GetParent(4);GetSon(4);foreach(testtinlist.Distinct(newtestEquality()))Response.Write(t.MenuID+"=="+t.MenuName+"<br/>");}publicvoidGetParent(intMenuID){DataRow[]row=(ViewState["Table"]asDataTable).Select("MenuID="+MenuID);foreach(DataRowrinrow){list.Add(newtest(){MenuID=Convert.ToInt32(r["MenuID"]),MenuName=r["MenuName"].ToString()});if(!r.IsNull("ParentMenuID"))GetParent(Convert.ToInt32(r["ParentMenuID"]));}}publicvoidGetSon(intParentMenuID){DataRow[]row=(ViewState["Table"]asDataTable).Select("ParentMenuID="+ParentMenuID);foreach(DataRowrinrow){list.Add(newtest(){MenuID=Convert.ToInt32(r["MenuID"]),MenuName=r["MenuName"].ToString()});if(!r.IsNull("MenuID"))GetSon(Convert.ToInt32(r["MenuID"]));}}publicvoidGetDataTable(){DataTabledt=newDataTable();using(SqlConnectionconn=newSqlConnection()){conn.ConnectionString=ConfigurationManager.ConnectionStrings["testConnection"].ConnectionString;SqlDataAdapterda=newSqlDataAdapter("selectMenuID,MenuName,ParentMenuIDfromtest",conn);da.Fill(dt);}ViewState["Table"]=dt;}}publicclasstestEquality:IEqualityComparer<test>{#regionIEqualityComparer<test>成员publicboolEquals(testx,testy){if(x.MenuID==y.MenuID&&x.MenuName==y.MenuName)returntrue;elsereturnfalse;}publicintGetHashCode(testobj){returnobj.MenuID+obj.MenuName.GetHashCode();}#endregion}publicclasstest{publicintMenuID{get;set;}publicstringMenuName{get;set;}publicintParentMenuID{get;set;}}