SELECT * FROM BranchInfo b
SELECT * FROM DepartInfo d
SELECT * FROM UserInfo u
我的查询结果:
SELECT b.BranchId ,b.BranchName,d.DepartName,u.UserId
FROM BranchInfo b,DepartInfo d,UserInfo u
WHERE b.BranchId=d.BranchId AND u.DepartId=d.DepartId
现在需要达到这样的结果:
要求是只读取一次数据库
把所需要的数据读取到datatable中
在线等待 。。。。。
谢谢了
用游标吧
试试CTE
http://www.cnblogs.com/downmoon/archive/2009/10/23/1588405.html
或自己封装带有Group功能的DataSource
http://www.cnblogs.com/downmoon/archive/2008/08/26/1276538.html
或DataRelation
http://www.cnblogs.com/downmoon/archive/2009/12/27/1633302.html
这个应该很简单把,动态树啊
不过你最后拉出来的资料这样不好实现,最好改成
ID TreeItem Flag ParentID
1 电力研究院 0 0
2 R&D部 1 1
3 电力部 1 1
4 admin 2 2
这里就要讲一下 Flag是对应相关图片的作用,下面付代码 仅做参考
protected void GetMenuTree()
{
sqlStr = "(select a.* from SysMenu a,SysUserMenu b,SysUser c where a.MenuID=b.MenuID and b.UserAcc=c.UserAcc and c.UserAcc='" + Session["UserAcc"].ToString().Trim() + "' and a.Enabled=1 and c.Enabled=1";
sqlStr = sqlStr + " union ";
sqlStr = sqlStr + " select a.* from SysMenu a,SysRoleMenu b,SysRole c,SysUser d";
sqlStr = sqlStr + " where a.MenuID=b.MenuID and b.RoleID=c.RoleID and c.RoleID=d.RoleID and d.UserAcc='" + Session["UserAcc"].ToString().Trim() + "' and a.Enabled=1 and c.Enabled=1 and d.Enabled=1)";
sqlStr = sqlStr + " order by Sort";
dt = Utils.ExecuteQueryT(sqlStr, connStr);
CreateTree(dt, null, "0");
}
private void CreateTree(DataTable dt, TreeNode ParNode, string strFileID)
{
DataView dv = new DataView(dt);
dv.RowFilter = "SJMenuID='" + strFileID + "'";
foreach (DataRowView rv in dv)
{
TreeNode node = new TreeNode();
node.Text = rv["MenuName"].ToString();
node.Value = rv["MenuName"].ToString();
if (rv["MenuKinds"].ToString().Trim() == "文件夹")
{
node.ImageUrl = "~/image/folderopen.gif";
node.SelectAction = TreeNodeSelectAction.Expand;
}
else
{
node.ImageUrl = "~/image/file.gif";
node.Target = "fsMain";
node.NavigateUrl = rv["MenuAddress"].ToString();
}
if (ParNode == null)
{
node.Expanded = false;
TreeView1.Nodes.Add(node);
}
else
{
ParNode.ChildNodes.Add(node);
}
CreateTree(dt, node, rv["MenuID"].ToString());
}
网上有这方面的源码,LZ自己下一个看下
http://www.51aspx.com/CV/SqlTree/
这是园子里面朋友转的
http://www.cnblogs.com/taihom/archive/2009/05/08/taihom.html
其实很简单了,先根据BranchInfo表创建一级节点,然后遍历一级节点,根据DepartInfo表创建二级节点,之后再遍历二级节点,根据UserInfo表创建三级节点。连递归都不用