/// <summary> /// 添加服务器列表 /// </summary> /// <param name="servers"></param> public static void AddServerList(Servers servers) { var ser = ServerList.Where(p => p.ID == servers.ID); if (ser.Count() > 0) { ServerList.Remove(ser.First()); } ServerList.Add(servers); }
/// <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(); }
private void Form_Database_Load(object sender, EventArgs e) { Form.CheckForIllegalCrossThreadCalls = false; var serverList = new Common.Config_Servers().GetAll(); foreach (var server in serverList) { Model.Servers ser = new Servers(); ser.DatabaseName = server.Database; ser.ID = server.Name; ser.Name = server.ServerName; ser.Password = server.Pwd; ser.Port = server.Port.IsInt() ? server.Port.ToInt() : -1; ser.Server = server.ServerName; Model.DatabaseType dbtype; ser.Type = Enum.TryParse(server.Type, out dbtype) ? dbtype : DatabaseType.Empty; ser.UserID = server.Uid; Common.Config.AddServerList(ser); TreeNode RootNode = new TreeNode(); RootNode.Name = server.ServerName; RootNode.Text = string.Format("{0}({1}{2})", server.ServerName, server.Type.ToString(), server.Uid.IsNullOrEmpty() ? "" : string.Format("-{0}", server.Uid)); RootNode.ImageIndex = 0; RootNode.SelectedImageIndex = 0; RootNode.Tag = new Model.TreeNodeTag() { Type = TreeNodeType.Server, Tag = ser }; treeView1.Nodes.Add(RootNode); } }
private void CreateToDir() { List <TreeNode> NodeList = Form1.form_Database.GetTreeView1Selected(); if (NodeList.Count == 0) { return; } TreeNode serverNode = Form1.form_Database.GetRoot(NodeList.First()); TreeNode dbNode = NodeList.First().Parent.Parent; List <Model.BuilderMethods> methods = new List <Model.BuilderMethods>(); if (checkBox_add.Checked) { methods.Add(Model.BuilderMethods.Add); } if (checkBox_count.Checked) { methods.Add(Model.BuilderMethods.Count); } if (checkBox_delete.Checked) { methods.Add(Model.BuilderMethods.Delete); } if (checkBox_exists.Checked) { methods.Add(Model.BuilderMethods.Exists); } if (checkBox_getall.Checked) { methods.Add(Model.BuilderMethods.SelectAll); } if (checkBox_getbykey.Checked) { methods.Add(Model.BuilderMethods.SelectByKey); } if (checkBox_update.Checked) { methods.Add(Model.BuilderMethods.Update); } Model.Servers server = (Model.Servers)((Model.TreeNodeTag)serverNode.Tag).Tag; Business.CreateCode CreateCode = new Business.CreateCode(server.Type); Model.CodeCreate param = new Model.CodeCreate(); param.DbName = ((Model.TreeNodeTag)dbNode.Tag).Tag.ToString(); param.NameSpace = ""; param.NameSpace1 = ""; param.ServerID = server.ID; param.BuilderType = this.radioButton1.Checked ? Model.BuilderType.Default : Model.BuilderType.Factory; param.MethodList = methods; param.CNSC = new Common.Config_NameSpaceClass().GetDefault(); Business.CreateCode CreateCodeInstince = new Business.CreateCode(server.Type); StreamWriter sw; string FileName = string.Empty; foreach (TreeNode node in NodeList) { param.TableName = ((Model.TreeNodeTag)node.Tag).Tag.ToString(); param.ClassName = param.TableName; //生成实体类 FileName = Common.Func.ExistsDirectory(string.Format("{0}\\{1}.{2}\\{3}{4}.cs", this.textBox_dir.Text, param.NameSpace, param.CNSC.Model, param.NameSpace1.IsNullOrEmpty() ? "" : param.NameSpace1 + "\\", param.ClassName)); sw = File.CreateText(FileName); sw.Write(CreateCodeInstince.GetModelClass(param)); sw.Close(); sw.Dispose(); label8.Text = string.Format("生成文件:{0}", FileName); //生成数据类 FileName = Common.Func.ExistsDirectory(string.Format("{0}\\{1}.{2}\\{3}{4}.cs", this.textBox_dir.Text, param.NameSpace, param.CNSC.Data, param.NameSpace1.IsNullOrEmpty() ? "" : param.NameSpace1 + "\\", param.ClassName)); sw = File.CreateText(FileName); sw.Write(CreateCodeInstince.GetDataClass(param)); sw.Close(); sw.Dispose(); label8.Text = string.Format("生成文件:{0}", FileName); //生成业务类 FileName = Common.Func.ExistsDirectory(string.Format("{0}\\{1}.{2}\\{3}{4}.cs", this.textBox_dir.Text, param.NameSpace, param.CNSC.Business, param.NameSpace1.IsNullOrEmpty() ? "" : param.NameSpace1 + "\\", param.ClassName)); sw = File.CreateText(FileName); sw.Write(CreateCodeInstince.GetBusinessClass(param)); sw.Close(); sw.Dispose(); label8.Text = string.Format("生成文件:{0}", FileName); if (param.BuilderType == Model.BuilderType.Factory) { //生成接口类 FileName = Common.Func.ExistsDirectory(string.Format("{0}\\{1}.{2}\\{3}{4}.cs", this.textBox_dir.Text, param.NameSpace, param.CNSC.Interface, param.NameSpace1.IsNullOrEmpty() ? "" : param.NameSpace1 + "\\", param.ClassName)); sw = File.CreateText(FileName); sw.Write(CreateCodeInstince.GetInterfaceClass(param)); sw.Close(); sw.Dispose(); label8.Text = string.Format("生成文件:{0}", FileName); //生成工厂类 FileName = Common.Func.ExistsDirectory(string.Format("{0}\\{1}.{2}\\{3}{4}.cs", this.textBox_dir.Text, param.NameSpace, param.CNSC.Factory, param.NameSpace1.IsNullOrEmpty() ? "" : param.NameSpace1 + "\\", param.ClassName)); sw = File.CreateText(FileName); sw.Write(CreateCodeInstince.GetFactoryClass(param)); sw.Close(); sw.Dispose(); label8.Text = string.Format("生成文件:{0}", FileName); } } MessageBox.Show("生成完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); this.label8.Text = "生成已完成"; this.button6.Enabled = true; }
private void CreateCode() { TreeNode dbNode = node.Parent.Parent; TreeNode serverNode = Form1.form_Database.GetRoot(node); if (dbNode == null || serverNode == null) { return; } List <Model.BuilderMethods> methods = new List <Model.BuilderMethods>(); if (checkBox_add.Checked) { methods.Add(Model.BuilderMethods.Add); } if (checkBox_count.Checked) { methods.Add(Model.BuilderMethods.Count); } if (checkBox_delete.Checked) { methods.Add(Model.BuilderMethods.Delete); } if (checkBox_exists.Checked) { methods.Add(Model.BuilderMethods.Exists); } if (checkBox_getall.Checked) { methods.Add(Model.BuilderMethods.SelectAll); } if (checkBox_getbykey.Checked) { methods.Add(Model.BuilderMethods.SelectByKey); } if (checkBox_update.Checked) { methods.Add(Model.BuilderMethods.Update); } Model.Servers server = (Model.Servers)((Model.TreeNodeTag)serverNode.Tag).Tag; Business.CreateCode CreateCode = new Business.CreateCode(server.Type); Model.CodeCreate param = new Model.CodeCreate(); param.ClassName = this.textBox2.Text.IsNullOrEmpty() ? ((Model.TreeNodeTag)node.Tag).Tag.ToString() : this.textBox2.Text.Trim(); param.DbName = ((Model.TreeNodeTag)dbNode.Tag).Tag.ToString(); param.NameSpace = ""; param.NameSpace1 = ""; param.ServerID = server.ID; param.TableName = ((Model.TreeNodeTag)node.Tag).Tag.ToString(); param.BuilderType = this.radioButton1.Checked ? Model.BuilderType.Default : Model.BuilderType.Factory; param.MethodList = methods; param.CNSC = new Common.Config_NameSpaceClass().GetDefault(); Form_Code_Area fca_model = new Form_Code_Area(CreateCode.GetModelClass(param), string.Format("实体类({0})", param.TableName)); fca_model.Show(Form1.Instance.dockPanel1); Form_Code_Area fca_data = new Form_Code_Area(CreateCode.GetDataClass(param), string.Format("数据类({0})", param.TableName)); fca_data.Show(Form1.Instance.dockPanel1); Form_Code_Area fca_business = new Form_Code_Area(CreateCode.GetBusinessClass(param), string.Format("业务类({0})", param.TableName)); fca_business.Show(Form1.Instance.dockPanel1); if (param.BuilderType == Model.BuilderType.Factory) { Form_Code_Area fca_interface = new Form_Code_Area(CreateCode.GetInterfaceClass(param), string.Format("接口类({0})", param.TableName)); fca_interface.Show(Form1.Instance.dockPanel1); Form_Code_Area fca_factory = new Form_Code_Area(CreateCode.GetFactoryClass(param), string.Format("工厂类({0})", param.TableName)); fca_factory.Show(Form1.Instance.dockPanel1); } this.Close(); }
/// <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(); }