コード例 #1
0
        private bool test_link_mysql(out string errMsg)
        {
            errMsg = string.Empty;
            string server   = this.comboBox_mysql_server.Text;
            string userid   = this.textBox_mysql_uid.Text;
            string userpass = this.textBox_mysql_pwd.Text;
            string prot     = this.textBox_mysql_port.Text;

            if (server.IsNullOrEmpty())
            {
                errMsg = "服务器不能为空";
                return(false);
            }
            else if (userid.IsNullOrEmpty())
            {
                errMsg = "登录名不能为空";
                return(false);
            }

            /*else if (userpass.IsNullOrEmpty())
             * {
             *  errMsg = "密码不能为空";
             *  return false;
             * }*/
            else if (this.BLL_Database == null)
            {
                errMsg = "数据库类型为空";
                return(false);
            }

            Common.Config.AddServerList(new Servers()
            {
                ID           = string.Format("{0}({1})", server.Trim(), dbType.ToString()),
                Name         = server.Trim(),
                Type         = dbType,
                DatabaseName = "",
                Password     = userpass.Trim(),
                Server       = server.Trim(),
                UserID       = userid.Trim(),
                Port         = prot.IsInt() ? prot.ToInt() : -1
            });

            return(BLL_Database.TestDatabaseConnnection(string.Format("{0}({1})", server.Trim(), dbType.ToString()), out errMsg));
        }
コード例 #2
0
        /// <summary>
        /// 加载下级节点
        /// </summary>
        private void AddNodes(bool isRefresh = false)
        {
            TreeNode selNode = tvDatabase.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();
        }