Пример #1
0
        /// <summary>
        /// 注销数据库
        /// </summary>
        public void RemoveServer()
        {
            if (treeView1.SelectedNode == null)
            {
                return;
            }
            TreeNode rootNode = GetRoot(treeView1.SelectedNode);

            if (rootNode == null)
            {
                return;
            }
            Model.TreeNodeTag tag = (Model.TreeNodeTag)rootNode.Tag;
            if (tag.Type != TreeNodeType.Server)
            {
                return;
            }
            Model.Servers server = (Model.Servers)tag.Tag;
            new Common.Config_Servers().Delete(server.ID);
            rootNode.Remove();
        }
Пример #2
0
        /// <summary>
        /// 加载下级节点
        /// </summary>
        private void AddNodes(bool isRefresh = false)
        {
            TreeNode selNode = treeView1.SelectedNode;

            if (selNode == null)
            {
                return;
            }
            if (selNode.Nodes.Count > 0 && !isRefresh)
            {
                return;
            }

            selNode.Nodes.Clear();
            TreeNode rootNode = GetRoot(selNode);

            if (rootNode == null)
            {
                return;
            }
            Model.TreeNodeTag rootNodeTag = (Model.TreeNodeTag)rootNode.Tag;
            if (!(rootNodeTag.Tag is Model.Servers))
            {
                return;
            }
            Model.Servers     server   = (Model.Servers)rootNodeTag.Tag;
            Model.TreeNodeTag nodeTag  = (Model.TreeNodeTag)selNode.Tag;
            Business.Database Database = new Business.Database(server.Type);
            string            msg;

            if (!Database.TestDatabaseConnnection(server.ID, out msg))
            {
                MessageBox.Show(msg, "连接失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            switch (nodeTag.Type)
            {
            case TreeNodeType.Server:     //服务器加载数据库
                var dbs = Database.GetDatabases(server.ID);
                foreach (var db in dbs)
                {
                    TreeNode dbNode = new TreeNode();
                    dbNode.Name               = db;
                    dbNode.Text               = db;
                    dbNode.ImageIndex         = 1;
                    dbNode.SelectedImageIndex = 1;
                    dbNode.Tag = new Model.TreeNodeTag()
                    {
                        Type = TreeNodeType.DataBase, Tag = db
                    };
                    selNode.Nodes.Add(dbNode);
                }
                break;

            case TreeNodeType.DataBase:     //数据库加载表视图节点
                //添加表节点
                TreeNode tblNode = new TreeNode();
                tblNode.Name = "表";
                tblNode.Text = "表";
                tblNode.Tag  = new Model.TreeNodeTag()
                {
                    Type = TreeNodeType.TableNode, Tag = nodeTag.Tag.ToString()
                };
                tblNode.ImageIndex         = 4;
                tblNode.SelectedImageIndex = 4;
                selNode.Nodes.Add(tblNode);

                //添加视图节点
                TreeNode viewNode = new TreeNode();
                viewNode.Name = "视图";
                viewNode.Text = "视图";
                viewNode.Tag  = new Model.TreeNodeTag()
                {
                    Type = TreeNodeType.ViewNode, Tag = nodeTag.Tag.ToString()
                };
                viewNode.ImageIndex         = 4;
                viewNode.SelectedImageIndex = 4;
                selNode.Nodes.Add(viewNode);
                break;

            case TreeNodeType.TableNode:     //表节点加载表
                var tables = Database.GetTables(server.ID, nodeTag.Tag.ToString());
                foreach (var table in tables)
                {
                    TreeNode tblNode1 = new TreeNode();
                    tblNode1.Name               = table.Name;
                    tblNode1.Text               = table.Name;
                    tblNode1.ImageIndex         = 2;
                    tblNode1.SelectedImageIndex = 2;
                    tblNode1.Tag = new Model.TreeNodeTag()
                    {
                        Type = TreeNodeType.Table, Tag = table.Name
                    };
                    selNode.Nodes.Add(tblNode1);
                }
                break;

            case TreeNodeType.ViewNode:     //视图节点加载视图
                var views = Database.GetViews(server.ID, nodeTag.Tag.ToString());
                foreach (var view in views)
                {
                    TreeNode viewNode1 = new TreeNode();
                    viewNode1.Name               = view.Name;
                    viewNode1.Text               = view.Name;
                    viewNode1.ImageIndex         = 2;
                    viewNode1.SelectedImageIndex = 2;
                    viewNode1.Tag = new Model.TreeNodeTag()
                    {
                        Type = TreeNodeType.View, Tag = view.Name
                    };
                    selNode.Nodes.Add(viewNode1);
                }
                break;

            case TreeNodeType.View:
            case TreeNodeType.Table:     //表加载字段
                var fields = Database.GetFields(server.ID, ((Model.TreeNodeTag)selNode.Parent.Tag).Tag.ToString(), ((Model.TreeNodeTag)selNode.Tag).Tag.ToString());
                foreach (var field in fields)
                {
                    TreeNode fldNode = new TreeNode();
                    fldNode.Name               = field.Name;
                    fldNode.Text               = string.Format("{0}({1}{2},{3})", field.Name, field.Type, field.Length != -1 ? "(" + field.Length.ToString() + ")" : "", field.IsNull ? "null" : "not null");
                    fldNode.ImageIndex         = field.IsPrimaryKey ? 5 : 3;
                    fldNode.SelectedImageIndex = field.IsPrimaryKey ? 5 : 3;
                    fldNode.Tag = new Model.TreeNodeTag()
                    {
                        Type = TreeNodeType.Field, Tag = field
                    };
                    selNode.Nodes.Add(fldNode);
                }
                break;
            }
            selNode.Expand();
        }