/// <summary> /// 修改表结构 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnModify_Click(object sender, EventArgs e) { #region 验证 if (dgvClassInfo.Rows.Count <= 0) { return; } if (dgvClassInfo.DataSource == null) { return; } #endregion try { List <AlterTableModel> lstModels = new List <AlterTableModel>(); foreach (DataGridViewRow dgvr in dgvClassInfo.Rows) { if (Convert.ToInt32(dgvr.Cells[cSel.Name].Value) == 1) { if (string.IsNullOrEmpty(dgvr.Cells[cOperateType.Name].Value.ToString().Trim())) { throw new Exception($"请为字段[{dgvr.Cells[cName.Name].Value.ToString()}]选择操作类型"); } AlterTableModel model = new AlterTableModel(); model.Name = dgvr.Cells[cName.Name].Value.ToString(); model.IsPramaryKey = Convert.ToInt32(dgvr.Cells[cIsPramaryKey.Name].Value) == 1 ? true : false; model.IdentityValue = dgvr.Cells[cIdentityValue.Name].Value.ToString(); model.DataType = dgvr.Cells[cDataType.Name].Value.ToString(); model.DefaultValue = dgvr.Cells[cDefaultValue.Name].Value.ToString(); model.Constraint = dgvr.Cells[cConstraint.Name].Value.ToString(); model.OperateType = dgvr.Cells[cOperateType.Name].Value.ToString().Trim(); lstModels.Add(model); } } if (lstModels.Count <= 0) { throw new Exception($"请选择要修改的字段"); } string t4FilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, T4Consts.FILE_Folder, T4Consts.DB_TABLE_ALTER_FILE_NAME); Air.T4.Common.Model.Database.HostDatabase hostDatabase = new Air.T4.Common.Model.Database.HostDatabase(); hostDatabase.Name = this.txtDbName.Text.Trim(); DataBaseCoreHost host = new DataBaseCoreHost(hostDatabase, txtTableName.Text.Trim(), lstModels); string output = Path.Combine(txt_Output.Text, "Alter" + this.txtTableName.Text.Trim() + ".sql"); this.rtbScripts.Text = T4EngineHelper.ProcessTemplate(t4FilePath, host, output, LogWrite); MessageBox.Show("修改表结构的脚本生成完毕"); } catch (Exception ea) { MessageBox.Show(ea.Message); return; } }
/// <summary> /// 写入脚本 /// </summary> /// <param name="platform"></param> private void WriteCreateDBTableScript(int platform = 1) { string t4FilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, T4Consts.FILE_Folder, T4Consts.DB_TABLE_CREATE_FILE_NAME); HostDatabase hostDatabase = new HostDatabase(); hostDatabase.Name = txt_DatabaseName.Text; DataBaseCoreHost host = new DataBaseCoreHost(hostDatabase); string scriptFileName = string.Empty; switch (platform) { case 1: //.Net Standard { Air.CodeGeneration.Data.Core.Model.Database database = new Air.CodeGeneration.Data.Core.Model.Database() { Name = txt_DatabaseName.Text }; database.TableItems = new List <DatabaseTable>(); foreach (DataGridViewRow row in dgv_Dll.Rows) { if (row.Cells["Col_Name"].Value.ToString() == "全选") { continue; } if (row.Cells["Col_Sel"].Value.ToString() == "false") { continue; } scriptFileName = row.Cells["Col_Name"].Value.ToString(); string tableName = row.Cells["Col_Name"].Value.ToString(); T4EngineHelper.SetCoreDataBaseTableItems(tableName, database, _lst_Types); } if (database.TableItems.Count <= 0) { return; } // hostDatabase.TableItems = database.TableItems.MapTo(new List<HostDatabaseTable>()); // var mapper = new Mapper(Mapper.Configuration); hostDatabase.TableItems = Mapper.Map <List <HostDatabaseTable> >(database.TableItems); break; } case 2: //Framework { Air.CodeGeneration.Data.Model.Database database = new Air.CodeGeneration.Data.Model.Database() { Name = txt_DatabaseName.Text }; database.TableItems = new List <Air.CodeGeneration.Data.Model.DatabaseTable>(); foreach (DataGridViewRow row in dgv_Dll.Rows) { if (row.Cells["Col_Name"].Value.ToString() == "全选") { continue; } if (row.Cells["Col_Sel"].Value.ToString() == "false") { continue; } scriptFileName = row.Cells["Col_Name"].Value.ToString(); string tableName = row.Cells["Col_Name"].Value.ToString(); T4EngineHelper.SetDataBaseTableItems(tableName, database, _lst_Types); } if (database.TableItems.Count <= 0) { return; } //hostDatabase.TableItems = database.TableItems.MapTo(new List<HostDatabaseTable>()); hostDatabase.TableItems = Mapper.Map <List <HostDatabaseTable> >(database.TableItems); break; } default: throw new Exception($"参数[{nameof(platform)}]值有误!"); } string output = Path.Combine(txt_Output.Text, scriptFileName + ".sql"); rtb_Script.Text = T4EngineHelper.ProcessTemplate(t4FilePath, host, output, LogWrite); }