比如:“全国”分为“河北、山西、山东.......”,“河北”分为“石家庄、保定、邯郸......”“石家庄”分为“正定、巨鹿、藁城....”
.......
这样无限的分下去这样的数据库该怎么设计??
最好是效率高点,必须能查询出每个节点的父节点和子节点
而且随时可以添加删除修改
有多个顶级节点问题:这个表怎么用树形表TreeView来显示出来我自己做了个感觉太复杂了,效率也很低,求大家给个方案,越详细越好,最好有例子,感激不尽

解决方案 »

  1.   

    http://topic.csdn.net/u/20090619/16/98091f21-2a6f-4079-a1a2-e223c49b0495.html
    无限级树形结构(增、删、改、排序+TreeView显示)「功能齐全」 参考……
      

  2.   

    id name parentid 三个字段就够了
      

  3.   

    使用id加parentid比较通用,无限级别都可以表示出来,但要查找就麻烦了,比如想找到所有的县名字使用3楼这种编码方式在使用上比较方便,但是涉及删除再添加时,编码比较难计算,而且这种固定编码方式存在长度问题,用2位表示一个级别的话,最多能表示99个。所以楼主综合考虑表表结构
      

  4.   

    数据库设置为id,name,parent等
    通过parent查询子集
    参考
    参考
      

  5.   

      private void bind_tree(string ChildNodes, TreeNode tn)
        {       
            DataTable dt = tcbll.GetByClassPre(ChildNodes).Tables[0];        foreach (DataRow dr in dt.Rows)
            {
                TreeNode Node = new TreeNode();
                if (tn == null)
                {    //添加根节点
                    Node.Text = dr["ClassName"].ToString();                
                    this.TreeView1.Nodes.Add(Node);
                    bind_tree(dr["ClassID"].ToString(), Node);    //再次递归
                }
                else
                {   //添加当前节点的子节点
                    Node.Text = dr["ClassName"].ToString();
                    tn.ChildNodes.Add(Node);
                    bind_tree(dr["ClassID"].ToString(), Node);     //再次递归
                }
            }    }    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.bind_tree("0", null);
            }
        }<form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server" />
            <div>
                <asp:TreeView ID="TreeView1" runat="server" ShowLines="True">
                <NodeStyle Font-Size="12px" />
                </asp:TreeView>
            </div>
        </form>    
      

  6.   

      private void bind_tree(string ChildNodes, TreeNode tn)
        {       
            DataTable dt = tcbll.GetByClassPre(ChildNodes).Tables[0];        foreach (DataRow dr in dt.Rows)
            {
                TreeNode Node = new TreeNode();
                if (tn == null)
                {    //添加根节点
                    Node.Text = dr["ClassName"].ToString();                
                    this.TreeView1.Nodes.Add(Node);
                    bind_tree(dr["ClassID"].ToString(), Node);    //再次递归
                }
                else
                {   //添加当前节点的子节点
                    Node.Text = dr["ClassName"].ToString();
                    tn.ChildNodes.Add(Node);
                    bind_tree(dr["ClassID"].ToString(), Node);     //再次递归
                }
            }    }    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.bind_tree("0", null);
            }
        }<form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server" />
            <div>
                <asp:TreeView ID="TreeView1" runat="server" ShowLines="True">
                <NodeStyle Font-Size="12px" />
                </asp:TreeView>
            </div>
        </form>    
      

  7.   

    重点不在程序设计,而是数据结构,pid存储它的上级id:id     pid      name
    1      0        全国
    2      1        上海
    3      2        浦东
    4      2        徐汇
    5      1        江苏
    6      5        南京
    7      6        白下
      

  8.   

    查找用递归SQL 不算麻烦吧