/// <summary> /// 生成属性映射表 /// </summary> /// <param name="tableName">表名</param> private void CreatePropertyMapping(string tableName) { string filePath = Path.Combine(Application.StartupPath.ToString(), string.Concat("Database\\", dbType, "\\", conn.Database, "\\", tableName, ".xml")); string dirPath = filePath.Substring(0, filePath.LastIndexOf("\\")); //每次都应该重建映射文件 //如果映射文件存在,则退出。 //if (File.Exists(filePath)) //{ // return; //} //如果目录不存在,则创建。 if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } //生成映射文件内容 Table parseTable = new Table(); foreach (DataGridViewRow row in DataGrid_Columns.Rows) { ColumnSchema column = GetColumn(row.Cells["FieldName"].Value.ToString()); parseTable.Columns.Add(column); } Helper helper = new Helper(); CodeTemplate codeTemplate = new CodeTemplate("Public"); VelocityContext context = new VelocityContext(); context.Put("Helper", helper); context.Put("Table", parseTable); foreach (DataGridViewRow row in DataGrid_Properties.Rows) { context.Put(row.Cells[1].Value.ToString(), row.Cells[2].Value.ToString()); } string result = string.Empty; try { result = codeTemplate.ParseTemplate(context, "ColumnToProperty.tpl"); } catch (Exception ex) { RtbCode.Text = ex.Message; } //写文件 StreamWriter sw = new StreamWriter(filePath, false, Encoding.UTF8); sw.WriteLine(result); sw.Close(); sw.Dispose(); }
/// <summary> /// 代码预览按下 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_Code_Click(object sender, EventArgs e) { Table parseTable = new Table(); parseTable.Name = this.table.Name; parseTable.Database = this.table.Database; parseTable.Comments = this.table.Comments; foreach (DataGridViewRow row in DataGrid_Columns.Rows) { if (row.Cells[0].Value != null && row.Cells[0].Value.ToString() == "1") { ColumnSchema column = GetColumn(row.Cells["FieldName"].Value.ToString()); parseTable.Columns.Add(column); } } if (Tree_Template.SelectedNode == null || Tree_Template.SelectedNode.Text.IndexOf(".") < 0) { MessageBox.Show("请选择模板文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } Helper helper = new Helper(); CodeTemplate codeTemplate = new CodeTemplate(dbType); VelocityContext context = new VelocityContext(); context.Put("Helper", helper); context.Put("Table", parseTable); context.Put("CurrentTime", DateTime.Now); foreach (DataGridViewRow row in DataGrid_Properties.Rows) { context.Put(row.Cells[1].Value.ToString(), row.Cells[2].Value.ToString()); } try { RtbCode.Text = codeTemplate.ParseTemplate(context, GetNodePath(Tree_Template.SelectedNode)); } catch (Exception ex) { RtbCode.Text = ex.Message; } ShowContainer(2); }
/// <summary> /// 生成全部文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Btn_File_Click(object sender, EventArgs e) { Table parseTable = new Table(); parseTable.Name = this.table.Name; parseTable.Database = this.table.Database; parseTable.Comments = this.table.Comments; foreach (DataGridViewRow row in DataGrid_Columns.Rows) { if ((row.Cells[0].Value != null) && (row.Cells[0].Value.ToString() == "1")) { ColumnSchema column = this.GetColumn(row.Cells["FieldName"].Value.ToString()); parseTable.Columns.Add(column); } } Helper helper = new Helper(); CodeTemplate template = new CodeTemplate(this.dbType); VelocityContext context = new VelocityContext(); context.Put("Helper", helper); context.Put("Table", parseTable); context.Put("CurrentTime", DateTime.Now); foreach (DataGridViewRow row in DataGrid_Properties.Rows) { context.Put(row.Cells[1].Value.ToString(), row.Cells[2].Value.ToString()); } List <string> files = new List <string>(); foreach (TreeNode node in Tree_Template.Nodes) { this.BindTag(node, "", files); } string fileName = ""; string outDirInfo = string.Format(ConfigurationManager.AppSettings["Output"].ToString(), AppDomain.CurrentDomain.BaseDirectory); foreach (string tplFileName in files) { try { fileName = Path.Combine(outDirInfo, tplFileName); foreach (DataGridViewRow row in DataGrid_Properties.Rows) { fileName = fileName.Replace("${" + row.Cells[1].Value.ToString() + "}", row.Cells[2].Value.ToString()); } this.CreateDirectory(fileName); using (File.Create(fileName)) { } using (StreamWriter writer = new StreamWriter(fileName, false, Encoding.GetEncoding("utf-8"))) { writer.Write(template.ParseTemplate(context, tplFileName)); } } catch (Exception exception) { this.RtbCode.Text = exception.Message; } } this.RtbCode.SelectionStart = 0; this.RtbCode.ScrollToCaret(); this.RtbCode.Text = "输出目录: "; this.RtbCode.Text = this.RtbCode.Text + outDirInfo; this.RtbCode.Text = this.RtbCode.Text + "\r\n生成时间: "; this.RtbCode.Text = this.RtbCode.Text + DateTime.Now.ToString(); this.ShowContainer(2); }