/// <summary> /// 绑定表信息 /// </summary> /// <param name="dt"></param> /// <param name="isCompositeQueryDt"></param> /// <param name="sql"></param> public void BindTableInfo(DataTable dt, bool isCompositeQueryDt, string sql = "") { this.tabControlOpt.SelectedTab = this.tabPageTable; this.dgvTableInfo.DataSource = dt; if (isCompositeQueryDt) { this._codeBaseInfo = TableBll.GetCodeBaseInfoByDBTable(dt, true); this._codeBaseInfo.Sql = sql; this.txtClassName.Text = string.Empty; } else { this._codeBaseInfo = TableBll.GetCodeBaseInfoByDBTable(dt); this.txtClassName.Text = TableBll.GetClassName(dt.TableName); } }
private void Core() { var index = 1; var tables = this.lbTargetTable.Items; var tempList = this._selectedTemps; var codeBaseDict = new Dictionary <string, CodeBaseInfo>(); //step1 获取表结构信息 var taskArg1 = new TaskArg(); taskArg1.BeginAction = () => { foreach (string table in tables) { var dt = TableBll.GetDBTableInfo(this._dbInfo.ConnectionString, table); var codeBaseInfo = TableBll.GetCodeBaseInfoByDBTable(dt); codeBaseInfo.NameSpace = this.txtNameSpace.Text; codeBaseInfo.ClassName = TableBll.GetClassName(dt.TableName); codeBaseDict.Add(table, codeBaseInfo); Thread.Sleep(1); TaskCenter.Instance.ReportProgress( new TaskReportProgressArg { Index = index++, Count = tables.Count, Msg = string.Format("step1/3 获取 {0} 结构信息......", table) }); } return(null); }; taskArg1.EndAction = (result) => { index = 1; }; //step2 创建模板文件夹 var taskArg2 = new TaskArg(); taskArg2.BeginAction = () => { foreach (var temp in tempList) { //一个模板一个子文件夹 var savePath = Path.Combine(this.txtOutputDirectory.Text, temp.DirName); if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } Thread.Sleep(100); TaskCenter.Instance.ReportProgress( new TaskReportProgressArg { Index = index++, Count = tempList.Count, Msg = string.Format("step2/3 创建模板文件夹{0}......", temp.DirName) }); } return(null); }; taskArg2.EndAction = (result) => { index = 1; }; //step3 生成代码 var taskArg3 = new TaskArg(); taskArg3.BeginAction = () => { var list = tempList.Where(d => !d.IsRanOnlyOnceByBath); foreach (var temp in list) { foreach (string table in tables) { var codeBaseInfo = codeBaseDict[table]; codeBaseInfo.ClassNameExtend = temp.ClassNameExtend; var output = TableBll.CodeGenerator(codeBaseInfo, temp.Path); var msg = string.Format(@"{0}\{1}", temp.Name, table); if (output.StartsWith("error:")) { this._failureTableList.Add(msg); } else { var fileName = string.IsNullOrEmpty(temp.FileName) ? codeBaseInfo.ClassName : temp.FileName; fileName = string.Format("{0}{1}{2}", fileName, temp.ClassNameExtend, temp.FileNameExtend); //一个模板一个子文件夹 var savePath = Path.Combine(this.txtOutputDirectory.Text, temp.DirName); if (temp.IsCreateChildDirByTableName) { savePath = Path.Combine(savePath, codeBaseInfo.ClassName); if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } } savePath = Path.Combine(savePath, fileName); File.WriteAllText(savePath, output, Encoding.UTF8); this._successTableList.Add(msg); } Thread.Sleep(1); TaskCenter.Instance.ReportProgress( new TaskReportProgressArg { Index = index++, Count = list.Count() * tables.Count, Msg = string.Format("step3/3 生成代码{0}/{1}......", temp.Name, table) }); } } index = 1; //RanOnlyOnce list = tempList.Where(d => d.IsRanOnlyOnceByBath); foreach (var temp in list) { var output = TableBll.CodeGenerator("dts", codeBaseDict.Values.Select(d => d.ClassName).ToList(), temp.Path, null); var msg = temp.Name; if (output.StartsWith("error:")) { this._failureTableList.Add(msg); } else { var fileName = string.Format("{0}{1}{2}", temp.FileName, temp.ClassNameExtend, temp.FileNameExtend); var savePath = Path.Combine(this.txtOutputDirectory.Text, temp.DirName); savePath = Path.Combine(savePath, fileName); File.WriteAllText(savePath, output, Encoding.UTF8); this._successTableList.Add(msg); Thread.Sleep(1); TaskCenter.Instance.ReportProgress( new TaskReportProgressArg { Index = index++, Count = list.Count(), Msg = string.Format("step3/3 生成代码{0}......", temp.Name) }); } } return(null); }; taskArg3.EndAction = (result) => { this.Invoke(new MethodInvoker(() => { var msg = "导出完成"; //if (e.Error != null) //{ // msg = e.Error.Message; // this._successTableList.Clear(); // this._failureTableList.Clear(); //} //else if (e.Cancelled) // msg = "导出被取消!"; //else // msg = "导出完成!"; this.SetContorlsEnabled(true); var form = new BathCodeGeneratorResultForm(msg, this.txtOutputDirectory.Text, this._successTableList, this._failureTableList); form.ShowDialog(); })); }; TaskCenter.Instance.AddTask(taskArg1); TaskCenter.Instance.AddTask(taskArg2); TaskCenter.Instance.AddTask(taskArg3); TaskCenter.Instance.ExecuteTasks(); }