/// <summary> /// 删除多个 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DelMore_Define(object sender, RoutedEventArgs e) { var count = 0; var listTable = AppCache.GetDefineSoureTable(); var listColumn = AppCache.GetDefineColumnList(); foreach (var item in DefineTable.Items) { var box = Common.GetTemplateColumn <CheckBox>(DefineTable, 0, "defBox", item); if (box != null && box.IsChecked == true) { count++; var colItem = item as DefineSoureTable; listTable.RemoveAll(a => a.colId == colItem.colId); listColumn.RemoveAll(a => a.colId == colItem.colId); } } AppCache.SetDefineColumnList(listColumn); AppCache.SetDefineSoureTable(listTable); if (count == 0) { CodeBox.Show("请选择要删除的列", this); } else { DefineTable.ItemsSource = AppCache.GetDefineSoureTable(); Common.DataGridSort(DefineTable, "tabName", ListSortDirection.Ascending); } }
/// <summary> /// 删除多个 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DelMore_Item(object sender, RoutedEventArgs e) { var count = 0; var listFrom = AppCache.GetFromList(); foreach (var item in fromTable.Items) { var box = Common.GetTemplateColumn <CheckBox>(fromTable, 0, "fromBox", item); if (box != null && box.IsChecked == true) { count++; var colItem = item as FromItems; listFrom.RemoveAll(a => a.colId == colItem.colId); } } AppCache.SetFromList(listFrom); if (count == 0) { CodeBox.Show("请选择要删除的列", this); } else { fromTable.ItemsSource = AppCache.GetFromList(); Common.DataGridSort(fromTable, "tabName", ListSortDirection.Ascending); } }
/// <summary> /// 生成实体 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Build_Define(object sender, RoutedEventArgs e) { var table = new BaseTable(); var info = new EntityInfo(); table.tabComments = txtComments.Text.Trim(); table.tabName = txtName.Text.Trim(); info.columns = AppCache.GetDefineColumnList(); info.modelFile = txtFile; info.nameSpace = txtNameSpace.Text.Trim(); info.table = table; info.language = language.SelectionBoxItem.ToString(); info.isSerialize = (bool)isSerialize.IsChecked; info.isCheck = (bool)isCheck.IsChecked; info.isModel = (bool)isModel.IsChecked; if (!info.isCheck && !info.isSerialize && !info.isMap && !info.isModel) { CodeBox.Show("请选择模板", this); } info.columns = info.disColType(info.columns, info.language); if (Common.BuildCodeModel(info)) { CodeBox.Show("生成成功", this); AppCache.SetDefineSoureTable(null); AppCache.SetDefineColumnList(null); } else { CodeBox.Show("生成失败", this); } }
/// <summary> /// 显示 /// </summary> /// <param name="msg">消息</param> /// <returns></returns> public static void Show(string msg, Window owner) { var msgBox = new CodeBox(); msgBox.Message = msg; Common.OpenWin(msgBox, owner); }
/// <summary> /// 生成CHM /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Bulid_Chm(object sender, RoutedEventArgs e) { try { var path = string.Format("{0}chm", AppDomain.CurrentDomain.BaseDirectory); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } if (txtFile != "") { path = txtFile; } //生成list var list = new List <ChmModel>(); foreach (var item in Dtable.Items) { var box = Common.GetTemplateColumn <System.Windows.Controls.CheckBox>(Dtable, 0, "tabBox", item); if (box != null && box.IsChecked == true) { var model = new ChmModel(); model.tabComments = (item as BaseTable).tabComments; model.tabName = (item as BaseTable).tabName; model.columns = DataSchema.ColumnList(AppCache.GetBuildLink(), model.tabName) ?? new List <BaseColumn>(); model.columns = model.disColType(model.columns); list.Add(model); } } if (list.Count == 0) { CodeBox.Show("请选择要生成表", this); return; } Chm.CreateHhp(path); Chm.CreateHhc(path, list); Chm.CreateHhk(path, list); Chm.Compile(path, list); CodeBox.Show("生成成功", this); } catch (Exception ex) { CodeBox.Show("生成失败", this); } }
/// <summary> /// 测试数据库连接 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Conn_Click(object sender, RoutedEventArgs e) { dbConn = Common.GetConnStr(dbType, txtUserName, txtPwd, txtHostName, txtPort, txtServerName); if (DataSchema.CheckLink(dbType, dbConn)) { CodeBox.Show("连接成功!", this); } else { CodeBox.Show("连接失败!", this); } }
/// <summary> /// 生成表单 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Build_From(object sender, RoutedEventArgs e) { var info = new EntityInfo(); var list = new List <BaseColumn>(); AppCache.GetFromList().ForEach(a => { var temp = new BaseColumn(); temp.colId = a.colId; temp.fromName = a.fromName; temp.fromParam = a.fromParam; temp.fromType = a.fromType ?? "text"; temp.colLength = a.maxLength; temp.isNull = a.isNull; temp.colComments = a.colComments; temp.colName = a.colName; temp.colType = a.colType; list.Add(temp); }); info.columns = list; info.modelFile = txtFile; info.isFrom = true; info.fromName = fromName.Text; info.fromUrl = fromUrl.Text; info.language = "C#"; var table = new BaseTable(); table.tabName = tabName.Text; table.tabComments = ""; info.table = table; info.nameSpace = nameSpace.Text; var count = Common.BuildCodeModel(info, "cshtml") == true ? 1 : 0; info.isFrom = false; info.isCheck = true; info.columns = info.disColType(info.columns, info.language); count += Common.BuildCodeModel(info) == true ? 1 : 0; if (count == 2) { CodeBox.Show("生成成功", this); AppCache.SetFromList(null); } else { CodeBox.Show("生成失败", this); } }
/// <summary> /// 增加自定义实体 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Add_Define(object sender, RoutedEventArgs e) { try { var count = 0; var tabItem = Dtable.SelectedItem as BaseTable; var tableList = AppCache.GetDefineSoureTable(); var columnList = AppCache.GetDefineColumnList(); foreach (var item in Dcolumn.Items) { var box = Common.GetTemplateColumn <System.Windows.Controls.CheckBox>(Dcolumn, 0, "colBox", item); var colItem = item as BaseColumn; var DSTable = new DefineSoureTable(); if (box != null && box.IsChecked == true) { count++; if (!columnList.Exists(a => a.colId == colItem.colId) && !tableList.Exists(a => a.colId == colItem.colId)) { columnList.Add(colItem); DSTable.colId = colItem.colId; DSTable.colComments = colItem.colComments; DSTable.colName = colItem.colName; DSTable.tabComments = tabItem.tabComments; DSTable.tabName = tabItem.tabName; tableList.Add(DSTable); } box.IsChecked = false; } } AppCache.SetDefineSoureTable(tableList); AppCache.SetDefineColumnList(columnList); if (count == 0) { CodeBox.Show("请选择要增加的列", this); } else { CodeBox.Show("增加成功", this); } } catch { CodeBox.Show("增加失败", this); } }
/// <summary> /// 增加表单元素 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Add_FromItem(object sender, RoutedEventArgs e) { try { var count = 0; var fromList = AppCache.GetFromList(); foreach (var item in Dcolumn.Items) { var box = Common.GetTemplateColumn <System.Windows.Controls.CheckBox>(Dcolumn, 0, "colBox", item); var colItem = item as BaseColumn; if (box != null && box.IsChecked == true) { count++; if (!fromList.Exists(a => a.colId == colItem.colId)) { var from = new FromItems(); from.colId = colItem.colId; from.fromName = colItem.colComments; from.fromParam = colItem.colName; from.maxLength = colItem.colLength; from.isNull = colItem.isNull; from.colComments = colItem.colComments; from.colType = colItem.colType; from.colName = colItem.colName; fromList.Add(from); } box.IsChecked = false; } } AppCache.SetFromList(fromList); if (count == 0) { CodeBox.Show("请选择要增加的表单元素", this); } else { CodeBox.Show("增加成功", this); } } catch { CodeBox.Show("增加失败", this); } }
/// <summary> /// 查看表单 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Query_From(object sender, RoutedEventArgs e) { if (AppCache.GetFromList().Count == 0) { CodeBox.Show("表单元素为空", this); } else { //弹出查询表单元素 var from = new From(); from.fromTable.ItemsSource = AppCache.GetFromList(); Common.DataGridSort(from.fromTable, "tabName", ListSortDirection.Ascending); Common.OpenWin(from, this); } }
/// <summary> /// 查看自定义实体 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Query_Define(object sender, RoutedEventArgs e) { if (AppCache.GetDefineColumnList().Count == 0) { CodeBox.Show("自定义实体数据为空", this); } else { //弹出查询自定义列 var definForm = new QueryDefine(); definForm.DefineTable.ItemsSource = AppCache.GetDefineSoureTable(); Common.DataGridSort(definForm.DefineTable, "tabName", ListSortDirection.Ascending); Common.OpenWin(definForm, this); } }
/// <summary> /// 保存 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Save_Click(object sender, RoutedEventArgs e) { dbConn = Common.GetConnStr(dbType, txtUserName, txtPwd, txtHostName, txtPort, txtServerName); if (!DataSchema.CheckLink(dbType, dbConn)) { CodeBox.Show("连接数据库失败!", this); } else { var buildLink = Common.ControlsToData(dbType, txtHostName, txtUserName, txtPwd, txtPort, txtServerName, labServerName, true, txtLinkName); buildLink.connStr = dbConn; Common.SaveConfigLink(buildLink); AppCache.SetBuildLink(buildLink); this.Owner.Title = string.Format("数据工具-{0}", txtLinkName.Text); AppCache.SetTitle(this.Owner.Title); AppCache.SetOnLine(buildLink); this.Close(); } }
/// <summary> /// 生成建表语句 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Bulid_Table(object sender, RoutedEventArgs e) { var bat = new StringBuilder(); var path = string.Format("{0}sql", AppDomain.CurrentDomain.BaseDirectory); if (txtFile != "") { path = txtFile + "\\sql"; } if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } foreach (var item in Dtable.Items) { var box = Common.GetTemplateColumn <System.Windows.Controls.CheckBox>(Dtable, 0, "tabBox", item); if (box != null && box.IsChecked == true) { var sql = new StringBuilder(); sql.AppendFormat("create table {0}(\r\n", (item as BaseTable).tabName); var field = DataSchema.ColumnList(AppCache.GetBuildLink(), (item as BaseTable).tabName) ?? new List <BaseColumn>(); var i = 0; field.ForEach(a => { if (AppCache.GetBuildLink().dbType == DataDbType.MySql) { if (a.isNull == "是") { sql.AppendFormat("\t{0} {1}{2} comment {3}\r\n", a.colName, a.showType, i == field.Count - 1 ? "" : ",", a.colComments.Replace("'", "")); } else { sql.AppendFormat("\t{0} {1} not null{2} comment {3}\r\n", a.colName, a.showType, i == field.Count - 1 ? "" : ",", a.colComments.Replace("'", "")); } } else { if (a.isNull == "是") { sql.AppendFormat("\t{0} {1}{2}\r\n", a.colName, a.showType, i == field.Count - 1 ? "" : ","); } else { sql.AppendFormat("\t{0} {1} not null{2}\r\n", a.colName, a.showType, i == field.Count - 1 ? "" : ","); } } i++; }); if (AppCache.GetBuildLink().dbType == DataDbType.MySql) { sql.AppendFormat(")comment={0} ", (item as BaseTable).tabComments.Replace("'", "")); bat.AppendFormat("mysql -h {0} -u {1} -p {2} < @{3}.sql\r\n", AppCache.GetBuildLink().hostName, AppCache.GetBuildLink().userName, AppCache.GetBuildLink().userPwd, (item as BaseTable).tabName); } else { sql.Append(") "); } if (AppCache.GetBuildLink().dbType == DataDbType.SqlServer) { field.ForEach(a => { if (a.colComments != "") { sql.AppendFormat("execute sp_addextendedproperty 'MS_Description','{0}','user','dbo','table','{1}','column','{2}';\r\n", a.colComments.Replace("'", ""), (item as BaseTable).tabName, a.colName); } }); sql.AppendFormat("execute sp_addextendedproperty 'MS_Description','{0}','user','dbo','table','{1}',null,null;\r\n", (item as BaseTable).tabComments.Replace("'", ""), (item as BaseTable).tabName); bat.AppendFormat("sqlcmd -U {0} -P {1} -i @{2}.sql\r\n", AppCache.GetBuildLink().userName, AppCache.GetBuildLink().userPwd, (item as BaseTable).tabName); } if (AppCache.GetBuildLink().dbType == DataDbType.Oracle) { sql.Append("\r\n tablespace USERS pctfree 10 initrans 1 maxtrans 255 storage(initial 64K minextents 1 maxextents unlimited);\r\n"); field.ForEach(a => { if (a.colComments != "") { sql.AppendFormat("comment on column {0}.{1} is '{2}'; \r\n", (item as BaseTable).tabName, a.colName, a.colComments.Replace("'", "")); } }); sql.AppendFormat("comment on table {0} is '{1}';\r\n", (item as BaseTable).tabName, (item as BaseTable).tabComments.Replace("'", "")); bat.AppendFormat("sqlplus {0}/{1}@{2} @{3}.sql> CreateTable.log\r\n", AppCache.GetBuildLink().userName, AppCache.GetBuildLink().userPwd, AppCache.GetBuildLink().serverValue, (item as BaseTable).tabName); } File.WriteAllText(string.Format("{0}/{1}.sql", path, (item as BaseTable).tabName), sql.ToString(), Encoding.UTF8); } } bat.Append("exit;"); if (AppCache.GetBuildLink().dbType == DataDbType.Oracle) { File.WriteAllText(string.Format("{0}/Oracle.bat", path), bat.ToString(), Encoding.UTF8); } if (AppCache.GetBuildLink().dbType == DataDbType.MySql) { File.WriteAllText(string.Format("{0}/MySql.bat", path), bat.ToString(), Encoding.UTF8); } if (AppCache.GetBuildLink().dbType == DataDbType.SqlServer) { File.WriteAllText(string.Format("{0}/SqlServer.bat", path), bat.ToString(), Encoding.UTF8); } CodeBox.Show("生成成功", this); }
/// <summary> /// 生成实体 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Build_Model(object sender, RoutedEventArgs e) { var list = new List <EntityInfo>(); //生成list foreach (var item in Dtable.Items) { var box = Common.GetTemplateColumn <System.Windows.Controls.CheckBox>(Dtable, 0, "tabBox", item); if (box != null && box.IsChecked == true) { var entiy = new EntityInfo(); entiy.table = item as BaseTable; entiy.language = language.SelectionBoxItem.ToString(); entiy.columns = DataSchema.ColumnList(AppCache.GetBuildLink(), entiy.table.tabName) ?? new List <BaseColumn>(); entiy.columns = entiy.disColType(entiy.columns, entiy.language); entiy.isSerialize = (bool)isSerialize.IsChecked; entiy.isCheck = (bool)isCheck.IsChecked; entiy.isMap = (bool)isMap.IsChecked; entiy.isModel = (bool)isModel.IsChecked; entiy.isOldModel = (bool)isOldModel.IsChecked; if (DataDbType.Oracle == AppCache.GetBuildLink().dbType) { entiy.param = ":"; } if (DataDbType.MySql == AppCache.GetBuildLink().dbType) { entiy.param = "?"; } if (DataDbType.SqlServer == AppCache.GetBuildLink().dbType) { entiy.param = "@"; } if (!entiy.isCheck && !entiy.isSerialize && !entiy.isMap && !entiy.isModel && !entiy.isOldModel) { CodeBox.Show("请选择模板", this); return; } list.Add(entiy); } } if (list.Count == 0) { CodeBox.Show("请选择要生成表", this); return; } //执行生成实体 list.ForEach(a => { //命名空间 a.nameSpace = txtNameSpace.Text.Trim(); //生成路径 if (txtFile != "") { a.modelFile = txtFile; } //语言 a.language = language.SelectionBoxItem.ToString(); if (a.isMap) { Common.BuildCodeModel(a, "xml"); } else { Common.BuildCodeModel(a); } }); CodeBox.Show(string.Format("生成完成"), this); }