/// <summary> /// 获取CS文件 /// </summary> /// <param name="parentName"></param> /// <param name="subName"></param> /// <param name="template"></param> /// <param name="entity"></param> /// <returns></returns> private string GetCSFile(string parentName, string subName, string template, string entity = "Entity") { var csFile = string.Empty; try { List <DBStructure> infos = DBInfo.DBInfoList.Where(w => w.DBName == parentName).Select(s => s.TableStruList).FirstOrDefault(); var info = infos.Where(w => w.TableName == subName).FirstOrDefault() ?? null; if (info == null) { return(csFile); } Dictionary <string, object> dicInfo = new Dictionary <string, object>(); dicInfo.Add("Entity", info); dicInfo.Add("ClassLibrary", ABContent.SelectedProject.ProjectName); dicInfo.Add("FolderName", cbx_Folder.Text); var result = NVelocityHelper.ProcessTemplate(template, dicInfo); //string result = Engine.Razor.RunCompile(template, "template6111", null, dbs); razor 略卡 csFile = FilesHelper.Write(Path.Combine(ABContent.SelectedProject.ProjectDirectoryName, cbx_Folder.Text), subName + "Entity.cs", result); } catch (Exception ex) { OutputWindowHelper.OutPutMessage($"获取CS文件出现异常 \n 信息:{ex.Message} \n 堆栈:{ex.StackTrace}"); } return(csFile); }
public static string NodeAdd(string path, DBInfo info) { string flag = string.Empty; try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(path); XmlNode xnList = xmlDoc.SelectSingleNode("DBInfoList"); XmlElement xeRoot = xmlDoc.CreateElement("DBInfo"); foreach (PropertyInfo p in info.GetType().GetProperties()) { if ("TableStruList,DBLink".Contains(p.Name)) { continue; } XmlElement xe = xmlDoc.CreateElement(p.Name); xe.InnerText = p.GetValue(info).ToString(); xeRoot.AppendChild(xe); } xnList.AppendChild(xeRoot); xmlDoc.Save(path); } catch (Exception ex) { flag = $"新增数据库连接出现异常 \r\n 信息:{ex.Message}"; OutputWindowHelper.OutPutMessage($"添加数据库连接出现异常 \r\n 信息:{ex.Message} \r\n 堆栈:{ex.StackTrace}"); } if (flag.Contains("访问被拒绝") && NeedAuth) { FilesHelper.AddSecurityControll2File(path); flag = NodeAdd(path, info); } return(flag); }
public static string NodeRemove(string path, string nodeName) { string flag = string.Empty; try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(path); XmlNodeList xnList = xmlDoc.SelectNodes("DBInfoList/DBInfo"); if (xnList != null && xnList.Count > 0) { foreach (XmlNode node in xnList) { if (node["DBName"].InnerText == nodeName) { node.ParentNode.RemoveChild(node); break; } } } xmlDoc.Save(path); } catch (Exception ex) { flag = $"删除数据库连接出现异常 \r\n 信息:{ex.Message}"; OutputWindowHelper.OutPutMessage($"删除数据库连接出现异常 \r\n 信息:{ex.Message} \r\n 堆栈:{ex.StackTrace}"); } if (flag.Contains("访问被拒绝") && NeedAuth) { FilesHelper.AddSecurityControll2File(path); flag = NodeRemove(path, nodeName); } return(flag); }
public ABMainForm(AutoBuildEntityContent abContent) { InitializeComponent(); ABContent = abContent; DBInfo = GetDBLink.GetBDList(); BindData(DBInfo); OutputWindowHelper.OutPutMessage("数据已加载完成"); }
private void tv_DBList_AfterSelect(object sender, TreeViewEventArgs e) { try { if (e.Node.Parent == null) //判断选中节点是否有父节点 { return; } bool hasControl = false; foreach (Control ctrol in tabControl1.Controls) { if (ctrol.Name == e.Node.Name) { tabControl1.SelectedIndex = tabControl1.Controls.IndexOf(ctrol); hasControl = true; } } if (hasControl == false) { TabPage tabpage = new TabPage(e.Node.Name + " "); tabpage.Name = e.Node.Name; tabpage.AutoScroll = true; var data = DBInfo.DBInfoList.Where(w => w.DBName == e.Node.Parent.Name).FirstOrDefault(); var list = data.TableStruList.Where(w => w.TableName == e.Node.Name).ToList(); DataGridView dgv = new DataGridView() { Dock = DockStyle.Fill, AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill, DataSource = list.Select(s => new CHNDesc { 列名 = s.ColName, 类型 = s.ColType, 长度 = s.ColLen.ToString(), 自增 = s.ColIsinc, 主键 = s.ColIsPri, 允许空 = s.ColisNull, 默认值 = s.DefaultValue, 描述 = s.ColDesc }).ToList() }; tabpage.Controls.Add(dgv); this.tabControl1.Controls.Add(tabpage); tabControl1.SelectedTab = tabpage; } } catch (Exception ex) { OutputWindowHelper.OutPutMessage($"查看数据结构出现异常 \r\n 信息:{ex.Message} \r\n 堆栈:{ex.StackTrace}"); OutPutMsg($"查看数据结构出现异常 \r\n 信息:{ex.Message}"); } }
/// <summary> /// 获取选中的项目所有信息 /// </summary> /// <returns></returns> private SelectedProject GetSelectedProject() { SelectedProject projectInfo = null; try { var dte = (DTE)GetService(typeof(SDTE)); //获取选中项目信息 projectInfo = dte.GetSelectedProjectInfo(); } catch (Exception) { OutputWindowHelper.OutPutMessage("未选择对应的项目,注意鼠标焦点"); } return(projectInfo); }
/// <summary> /// 获取CS文件 /// </summary> /// <param name="parentName"></param> /// <param name="subName"></param> /// <param name="template"></param> /// <param name="entity"></param> /// <returns></returns> private string GetCSFile(string parentName, string subName, string template, List <DBStructure> dbStru = null, string sql = "", string entity = "Entity") { var csFile = string.Empty; try { List <DBStructure> strinfo = dbStru; if (dbStru == null || dbStru.Count == 0) { List <DBStructure> infos = DBInfo.DBInfoList.Where(w => w.DBName == parentName).Select(s => s.TableStruList).FirstOrDefault(); strinfo = infos == null ? null : infos.Where(w => w.TableName == subName).ToList(); if (strinfo == null && strinfo.Count() > 0) { return(csFile); } strinfo.ForEach(f => f.ColDesc = Regex.Replace(f.ColDesc, @"[\r\n]+", ";")); } Dictionary <string, object> dicInfo = new Dictionary <string, object>(); dicInfo.Add("Entity", strinfo); dicInfo.Add("NameSpace", BEContent.SelectedProject.ProjectName + (cbx_Folder.Text == "根文件" ? "" : $".{cbx_Folder.Text}")); dicInfo.Add("DBName", parentName); dicInfo.Add("TableName", subName); if (string.IsNullOrEmpty(sql) == false) { dicInfo.Add("SQL", sql); } var result = NVelocityHelper.ProcessTemplate(BEContent.SelectedProject.ProjectDirectoryName, template, dicInfo); csFile = FilesHelper.Write(Path.Combine(BEContent.SelectedProject.ProjectDirectoryName, cbx_Folder.Text == "根文件" ? "" : cbx_Folder.Text), subName + "Entity", result); } catch (Exception ex) { OutputWindowHelper.OutPutMessage($"获取CS文件出现异常 \r\n 信息:{ex.Message} \r\n 堆栈:{ex.StackTrace}"); OutPutMsg($"获取CS文件出现异常 \r\n 信息:{ex.Message}"); } return(csFile); }
/// <summary> /// 数据执行操作 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_Build_Click(object sender, EventArgs e) { try { List <string> csFileList = new List <string>(); var template = string.Empty; List <TreeNode> tNodeList = new List <TreeNode>(); if (tabControl1.SelectedTab != null && tabControl1.SelectedTab.Name == "tPage_SQL") { var sql = txtSqls.Text.Trim(); var link = DBInfo.DBInfoList.Where(w => w.DBName == tv_DBList.SelectedNode.Name).FirstOrDefault(); if (link == null) { OutPutMsg($"鼠标焦点不在库节点上,请重置焦点"); } DataColumnCollection colums = null; if (link.Type == DBEnum.SqlServer) { colums = SqlHelper.ExecuteDataTable(link?.DBLink, sql).Columns; } else { colums = MySqlHelper.ExecuteDataTable(link?.DBLink, sql).Columns; } List <DBStructure> infos = new List <DBStructure>(); template = File.ReadAllText(Path.Combine(BEContent.SelectedProject.ABEPath, "Templates", "ComboEntity.txt")); for (int i = 0; i < colums.Count; i++) { DBStructure stru = new DBStructure(); stru.ColName = colums[i].ColumnName; stru.ColType = colums[i].DataType.MapCsharpType(); infos.Add(stru); } var csFile = GetCSFile(tv_DBList.SelectedNode.Name, "NameReplace", template, infos, sql); csFileList.Add(csFile); } else { template = File.ReadAllText(Path.Combine(BEContent.SelectedProject.ABEPath, "Templates", "Entity.txt")); foreach (TreeNode tnode in this.tv_DBList.Nodes) { foreach (TreeNode node in tnode.Nodes) { if (node.Checked) { tNodeList.Add(node); var csFile = GetCSFile(node.Parent.Name, node.Name, template); csFileList.Add(csFile); } } } } BEContent.SelectedProject.ProjectDte.AddFilesToProject(csFileList); tNodeList.ForEach(f => f.Checked = false); txtSqls.Clear(); OutPutMsg($"数据执行操作成功,共生成{csFileList.Count}文件"); } catch (Exception ex) { OutputWindowHelper.OutPutMessage($"数据执行操作出现异常 \r\n 信息:{ex.Message} \r\n 堆栈:{ex.StackTrace}"); OutPutMsg($"数据执行操作出现异常 \r\n 信息:{ex.Message}"); } }