/// <summary> /// Model预览按钮事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnModel_Click(object sender, EventArgs e) { //1、判断用户是否选择了表 if (lsbRight.Items.Count == 0) { MessageBox.Show("请选择要操作的表!"); return; } string tabName = lsbRight.Items[0].ToString(); //2、判断命名空间输入是否为空 string ns = txtNameSpace.Text.Trim(); if (string.IsNullOrEmpty(ns)) { MessageBox.Show("请输入命名空间!"); return; } //3、其他 string front = txtFront.Text.Trim(); string author = txtAuthor.Text.Trim(); string consrt = GetConnStr(); string dataBase = txtSqlDataBase.Text.Trim(); string className = tabName.Replace(front, ""); //首字线大字 className = className.Substring(0, 1).ToUpper() + className.Substring(1); //获取Model模板代码 txtCode.Text = GenModel.GetAllCode(ns, tabName, author, className, consrt, dataBase); }
/// <summary> /// 一键批量生成的代码 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnGen_Click(object sender, EventArgs e) { //1、判断用户是否选择了表 if (lsbRight.Items.Count == 0) { MessageBox.Show("请选择要操作的表!"); return; } string tabName = lsbRight.Items[0].ToString(); //2、判断命名空间输入是否为空 string ns = txtNameSpace.Text.Trim(); if (string.IsNullOrEmpty(ns)) { MessageBox.Show("请输入命名空间!"); return; } //3、判断路径 string output = txtOutPath.Text.Trim(); if (output.ToString() == "") { output = "D:\\CodeHere\\"; txtOutPath.Text = output; } if (!Directory.Exists(output)) { try { Directory.CreateDirectory(output); } catch (Exception) { MessageBox.Show("请选择正确的生成目录!"); return; } } //当条件满足 #region 生成Model string output_model = ""; if (output.LastIndexOf("\\") == 0) { output_model = output + "Model\\"; } else { output_model = output + "\\Model\\"; } if (!Directory.Exists(output_model)) { Directory.CreateDirectory(output_model); } //3、其他 string front = txtFront.Text.Trim(); string author = txtAuthor.Text.Trim(); string conStr = GetConnStr(); string dataBase = txtSqlDataBase.Text.Trim(); string className = ""; foreach (string tableName in lsbRight.Items) { className = tableName.Replace(front, ""); //首字线大字 className = className.Substring(0, 1).ToUpper() + className.Substring(1); string filePath = output_model + className + ".cs"; StreamWriter sw1 = new StreamWriter(filePath, false); sw1.Write(GenModel.GetAllCode(ns, tableName, author, className, conStr, dataBase)); sw1.Flush(); sw1.Close(); sw1.Dispose(); } #endregion #region 生成DAL string output_dal = ""; if (output.LastIndexOf("\\") == 0) { output_dal = output + "DAL\\"; } else { output_dal = output + "\\DAL\\"; } if (!Directory.Exists(output_dal)) { Directory.CreateDirectory(output_dal); } foreach (string tableName in lsbRight.Items) { //txtCode显示第一张表 if (tableName == lsbRight.Items[0].ToString()) { if (txtCode.Text.Trim() == "") { txtCode.Text = GenDAL.GetDALCode(ns, tableName, author, className, conStr, dataBase); } } className = tableName.Replace(front, ""); //首字线大字 className = className.Substring(0, 1).ToUpper() + className.Substring(1); string filePath = output_dal + className + ".cs"; StreamWriter sw2 = new StreamWriter(filePath, false); sw2.Write(GenDAL.GetDALCode(ns, tableName, author, className, conStr, dataBase)); sw2.Flush(); sw2.Close(); sw2.Dispose(); } #endregion #region 数据库助手类 string filePath2 = output_dal + "DBHelper.cs"; StreamWriter sw3 = new StreamWriter(filePath2, false); sw3.Write(GenDAL.GetnSQLHelper(ns, conStr)); sw3.Flush(); sw3.Close(); sw3.Dispose(); #endregion #region 数据库帮助HTML文件 string output_sqlfile = ""; if (output.LastIndexOf("\\") > 0) { output_sqlfile = output + "\\"; } string path3 = output_sqlfile + txtSqlDataBase.Text.Trim() + "数据库表结构"; #endregion }