/// <summary> /// 添加一个服务器到tree /// </summary> /// <param name="serverID"></param> public void AddServer(string serverID) { Servers server = Common.Config.GetServer(serverID); if (server == null) { return; } treeView1.Nodes.RemoveByKey(server.ID); TreeNode RootNode = new TreeNode(); RootNode.Name = server.ID; RootNode.Text = string.Format("{0}({1}{2})", server.Name, server.Type.ToString(), server.UserID.IsNullOrEmpty() ? "" : string.Format("-{0}", server.UserID)); RootNode.ImageIndex = 0; RootNode.SelectedImageIndex = 0; RootNode.Tag = new Model.TreeNodeTag() { Type = TreeNodeType.Server, Tag = server }; treeView1.Nodes.Add(RootNode); Business.Database Database = new Business.Database(server.Type); //添加数据库 List<string> dbList = Database.GetDatabases(serverID); foreach (var db in dbList) { TreeNode dbNode = new TreeNode(); dbNode.Name = db; dbNode.Text = db; dbNode.Tag = new Model.TreeNodeTag() { Type = TreeNodeType.DataBase, Tag = db }; dbNode.ImageIndex = 1; dbNode.SelectedImageIndex = 1; RootNode.Nodes.Add(dbNode); //添加表节点 TreeNode tblNode = new TreeNode(); tblNode.Name = "表"; tblNode.Text = "表"; tblNode.Tag = new Model.TreeNodeTag() { Type = TreeNodeType.TableNode, Tag = db }; tblNode.ImageIndex = 4; tblNode.SelectedImageIndex = 4; dbNode.Nodes.Add(tblNode); //添加视图节点 TreeNode viewNode = new TreeNode(); viewNode.Name = "视图"; viewNode.Text = "视图"; viewNode.Tag = new Model.TreeNodeTag() { Type = TreeNodeType.ViewNode, Tag = db }; viewNode.ImageIndex = 4; viewNode.SelectedImageIndex = 4; dbNode.Nodes.Add(viewNode); } RootNode.Expand(); Form1.Instance.ShowServerList(); }
/// <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(); }
public Form_AddDatabase_SqlServer(Model.DatabaseType dbType) { InitializeComponent(); this.dbType = dbType; this.BLL_Database = new Business.Database(dbType); }
/// <summary> /// 添加一个服务器到tree /// </summary> /// <param name="serverID"></param> public void AddServer(string serverID) { Servers server = Common.Config.GetServer(serverID); if (server == null) { return; } treeView1.Nodes.RemoveByKey(server.ID); TreeNode RootNode = new TreeNode(); RootNode.Name = server.ID; RootNode.Text = string.Format("{0}({1}{2})", server.Name, server.Type.ToString(), server.UserID.IsNullOrEmpty() ? "" : string.Format("-{0}", server.UserID)); RootNode.ImageIndex = 0; RootNode.SelectedImageIndex = 0; RootNode.Tag = new Model.TreeNodeTag() { Type = TreeNodeType.Server, Tag = server }; treeView1.Nodes.Add(RootNode); Business.Database Database = new Business.Database(server.Type); //添加数据库 List <string> dbList = Database.GetDatabases(serverID); foreach (var db in dbList) { TreeNode dbNode = new TreeNode(); dbNode.Name = db; dbNode.Text = db; dbNode.Tag = new Model.TreeNodeTag() { Type = TreeNodeType.DataBase, Tag = db }; dbNode.ImageIndex = 1; dbNode.SelectedImageIndex = 1; RootNode.Nodes.Add(dbNode); //添加表节点 TreeNode tblNode = new TreeNode(); tblNode.Name = "表"; tblNode.Text = "表"; tblNode.Tag = new Model.TreeNodeTag() { Type = TreeNodeType.TableNode, Tag = db }; tblNode.ImageIndex = 4; tblNode.SelectedImageIndex = 4; dbNode.Nodes.Add(tblNode); //添加视图节点 TreeNode viewNode = new TreeNode(); viewNode.Name = "视图"; viewNode.Text = "视图"; viewNode.Tag = new Model.TreeNodeTag() { Type = TreeNodeType.ViewNode, Tag = db }; viewNode.ImageIndex = 4; viewNode.SelectedImageIndex = 4; dbNode.Nodes.Add(viewNode); } RootNode.Expand(); Form1.Instance.ShowServerList(); }
/// <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(); }