zoukankan      html  css  js  c++  java
  • TreeView绑定数据库收藏

    日志 > 个人日记
    发表于:2008年6月13日 17时51分13秒阅读(0)评论(0)特效:[图] 举报本文链接:http://user.qzone.qq.com/573149158/blog/1213350673
    新一篇: TextBox信纸样式显示数据库:
    公司:id(int)     company(varchar(50)
    部门:id(int)     deid(int)     department(varchar(50))
    班组:id(int)     coid(int)     class(varchar(50))
    前台:
    <asp:TreeView ID="TreeView1" runat="server" ShowLines="true" ExpandDepth="0" >
    ShowLines="true"..........................显示连接树的节点
    ExpandDepth=“0”...........................展开树的深度
    后台:
    方法一:
            string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["rizhi"].ConnectionString;
            protected void Page_Load(object sender, EventArgs e)
            ...{
                if (!IsPostBack)
                ...{
                    using (SqlConnection conn = new SqlConnection(strConn))
                    ...{
                        DataSet ds_company = new DataSet();//实例化数据集(公司)
                        SqlDataAdapter sda_company = new SqlDataAdapter("select * from [company]", conn);//实例化数据适配器(公司)
                        sda_company.Fill(ds_company, "company");//虚拟表填充到内存
                        for (int i = 0; i < ds_company.Tables["company"].Rows.Count; i++)//循环所有"公司"
                        ...{
                            TreeNode td_company = new TreeNode();//实例化公司节点
                            td_company.Text = ds_company.Tables["company"].Rows[i]["company"].ToString();//公司节点名称
                            TreeView1.Nodes.Add(td_company);//将公司节点添加至"树"
                            DataSet ds_department = new DataSet();//实例化数据集(部门)
                            SqlDataAdapter sda_department = new SqlDataAdapter("select * from [department] where [coid]=" + ds_company.Tables["company"].Rows[i]["id"], conn);//实例化数据库适配器(部门)
                            sda_department.Fill(ds_department, "department");//虚拟表填充到内存
                            for (int j = 0; j < ds_department.Tables["department"].Rows.Count; j++)//循环 ds.Tables["company"].Rows[i]["id"] 对应的所有部门
                            ...{
                                TreeNode td_department = new TreeNode();//实例化部门节点
                                td_department.Text = ds_department.Tables["department"].Rows[j]["department"].ToString();//部门节点名称
                                td_company.ChildNodes.Add(td_department);//填充部门节点至"树"

                                DataSet ds_class = new DataSet();//实例化数据集(班组)
                                SqlDataAdapter sda_class = new SqlDataAdapter("select * from [class] where [deid]=" + ds_department.Tables["department"].Rows[j]["id"], conn);//实例化数据库适配器(班组)
                                sda_class.Fill(ds_class, "class");//虚拟表填充到内存
                                for (int z = 0; z < ds_class.Tables["class"].Rows.Count; z++)//循环 ds_department.Tables["department"].Rows[j]["id"] 对应的所有部门
                                ...{
                                    TreeNode td_class = new TreeNode();//实例化班组节点
                                    td_class.Text = ds_class.Tables["class"].Rows[z]["class"].ToString();//班组节点名称
                                    td_department.ChildNodes.Add(td_class);//填充班组节点至"树"
                                }
                            }
                        }
                    }
                }
            }
        }
    方法二:
            string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["rizhi"].ConnectionString;
            protected void Page_Load(object sender, EventArgs e)
            ...{
                if (!IsPostBack)
                ...{
                    DataSet ds = new DataSet();
                    SqlConnection conn = new SqlConnection(strConn);
                    using (conn)
                    ...{
                        SqlDataAdapter sda_Company = new SqlDataAdapter("select [id],[company] from [company]", conn);
                        sda_Company.Fill(ds, "company");
                        TreeNode tn_Company;
                        TreeNode tn_Department;
                        TreeNode tn_Class;
                        foreach (DataRow company_row in ds.Tables["company"].Rows)
                        ...{
                            tn_Company = new TreeNode();
                            tn_Company.Text = company_row["company"].ToString();
                            tn_Company.Value = company_row["id"].ToString();
                            TreeView1.Nodes.Add(tn_Company);

                            SqlDataAdapter sda_Department = new SqlDataAdapter("select [id],[department] from [department] where coid=" + tn_Company.Value, conn);
                            sda_Department.Fill(ds, "department");
                            if (ds.Tables["department"].Rows.Count > 0)
                            ...{
                                foreach (DataRow department_row in ds.Tables["department"].Rows)
                                ...{
                                    tn_Department = new TreeNode();
                                    tn_Department.Text = department_row["department"].ToString();
                                    tn_Department.Value = department_row["id"].ToString();
                                    tn_Company.ChildNodes.Add(tn_Department);

                                    SqlDataAdapter sda_Class = new SqlDataAdapter("select [id],[class] from [class] where deid=" + tn_Department.Value, conn);
                                    sda_Class.Fill(ds, "class");
                                    if (ds.Tables["class"].Rows.Count > 0)
                                    ...{
                                        foreach (DataRow class_row in ds.Tables["class"].Rows)
                                        ...{
                                            tn_Class = new TreeNode();
                                            tn_Class.Text = class_row["class"].ToString();
                                            tn_Class.Value = class_row["id"].ToString();
                                            tn_Department.ChildNodes.Add(tn_Class);
                                        }
                                        ds.Tables["class"].Clear();
                                    }
                                    sda_Class.Dispose();
                                }
                                ds.Tables["department"].Clear();
                            }
                            sda_Department.Dispose();
                        }
                        sda_Company.Dispose();
                    }
                }
            }
    我遇见谁会有怎样的对白,我等的人她在多远的未来, 我听见风来自地铁和人海,我排着队拿着爱的号码牌……
  • 相关阅读:
    java基础
    mysql入门
    基础整理
    遍历列表的两种方式
    oracle常用操作
    DIV+CSS网页布局技巧实例1:设置网页整体居中的代码
    html+css 淘宝首页静态页面案例
    WEB前端开发规范文档+CSS命名规范
    day05-苏宁易购导航html
    day04-html
  • 原文地址:https://www.cnblogs.com/whj/p/1219554.html
Copyright © 2011-2022 走看看