///////////////////////以下生成 Model #region 多表时,生成实例类文件 /// <summary> /// 多表时,生成实例类文件 /// </summary> /// <param name="ds"></param> public string ToModels(bool confirmWrite, List <string> tablesName) { StringBuilder ret = new StringBuilder(); ProcTable pt = new ProcTable(); ProcString ps = new ProcString(); ps.SetTabs(strNameSpace); DataSet dsColumnsDescription = pt.GetTablesColumnDecription(tablesName); foreach (string tableName in tablesName) { //if (tableName == " JHPIX_PAT_MASTER_INDEX") // MessageBox.Show("JHPIX_PAT_MASTER_INDEX类的GetUpdateSQL方法特殊!不可自动生成"); string strClass = ""; TableInfo tableInfo = new TableInfo(strNameSpace, strPrefix, tableName); DataSet dsTableInfo = tableInfo.dsTableInfo; DataTable dtColumnType = dsTableInfo.Tables[1]; //DataColumnCollection dtcols = dtColumnType.Columns; DataTable dtColumnsDescription = null; string tableDecription = pt.GetTableDesciption(tableName); for (int i = 0; i < dsColumnsDescription.Tables.Count; i++) { dtColumnsDescription = dsColumnsDescription.Tables[i].Copy(); if (dtColumnsDescription.TableName == tableName) { dsColumnsDescription.Tables.RemoveAt(i); break; } } //缺少主键校验 strClass = pt.GetTable(dtColumnType, dtColumnsDescription , tableDecription , strPrefix, strNameSpace); ret.AppendLine(strClass); #region 写文件 if (confirmWrite) { string tem = path + "/Model/"; if (!Directory.Exists(tem)) { Directory.CreateDirectory(tem); } string className = ps.GetClassNameByTableName(tableName, strPrefix.Trim()); //类名大写 //className = ps.GetClassName(className);//lsj string fullPath = tem + className + ".cs"; //2021-2-2-lsj-首字母大写 Write(fullPath, strClass); } #endregion } return(ret.ToString()); }
/// <summary> /// 生成带Class(带成员)字符串 /// 实体类 /// </summary> /// <param name="strNameSpace">窗空间</param> /// <param name="strPrefix">表前缀</param> /// <param name="tableName">表名</param> /// <param name="strTableDescription">表描述</param> /// <param name="strMember">要加入的类成员</param> /// <param name="strClassBody">要加入的类主体</param> /// <returns></returns> public string GetCLassByModel(string strNameSpace, string strPrefix , string tableName , string strClassPrefix , string strMember , string strClassBody) { StringBuilder ret = new StringBuilder(); //类名大写 string strClassName = GetClassNameByTableName(tableName, strPrefix.Trim());//2021-2-2-lsj-类名不大写,和数据库同名 ProcTable pt = new ProcTable(); string strTableDescription = pt.GetTableDesciption(tableName); SetTabs(strNameSpace.Trim()); #region 写名称空间 { ret.AppendLine(strImportNameSpace); if (strNameSpace.Trim().Length > 0) { ret.AppendLine("namespace " + strNameSpace.Trim()); ret.AppendLine("{"); } #endregion #region 写类描述 if (strTableDescription.Length > 0) {//有表描述, ret.AppendLine(tabClass + "/// <summary>"); ret.AppendLine(tabClass + "/// " + strTableDescription); ret.AppendLine(tabClass + "/// <summary>"); } #endregion ret.AppendLine(tabClass + "public class " + strClassPrefix + strClassName); ret.AppendLine(tabClass + "{"); if (!string.IsNullOrEmpty(strMember)) { ret.AppendLine(tabMember + strMember); } ret.Append(strClassBody); ret.AppendLine(tabClass + "}"); #region 写名称空间 } if (strNameSpace.Trim().Length > 0) { ret.AppendLine("}"); } #endregion return(ret.ToString()); }
///////////////////////以上生成 Model ////////////////////////////以下生成 数据访问层 #region 生成单表数据访问层 /// <summary> /// 生成单表数据访问层 /// </summary> /// <param name="confirmWrite">是否生成文件</param> /// <returns></returns> public string ToDataAccess(bool confirmWrite, List <string> tablesName) { StringBuilder ret = new StringBuilder(); ProcTable pt = new ProcTable(); ProcString ps = new ProcString(); ps.SetTabs(strNameSpace); ps.strImportNameSpace = @"using System; using System.Data; using System.Collections.Generic; "; foreach (string tableName in tablesName) { string strMember = @"IDBOperator idb = DBOperator.GetInstance();"; string strClassBody = ps.GetDataAccessClassBody(tableName, strPrefix, strNameSpace); string str = ps.GetCLassByModel(strNameSpace, strPrefix, tableName, strADO, strMember, strClassBody); ret.Append(str); #region 写文件 if (confirmWrite) { string tem = path + "/ADO/"; string strClassName = ps.GetClassNameByTableName(tableName, strPrefix); if (!Directory.Exists(tem)) { Directory.CreateDirectory(tem); } string fullpath = tem + strADO + strClassName + ".cs"; Write(fullpath, ret.ToString()); } #endregion } return(ret.ToString()); }
/// <summary> /// 生成数据库文档 /// </summary> /// <param name="tablesName"></param> public void ToWord(List <string> tablesName) { ProcTable pt = new ProcTable(); DataSet dsColumnsDescription = pt.GetTablesColumnDecription(tablesName); string fullPath = path + "/数据库文档.doc"; ProcWord word = new ProcWord(); word.FileName = fullPath; foreach (string tableName in tablesName) { TableInfo tableInfo = new TableInfo(strNameSpace, strPrefix, tableName); DataSet dsTableInfo = tableInfo.dsTableInfo; //0表名 1列类型 2标识列 DataTable dtColumnType = tableInfo.ColumnType; //列名0 类型1 长度3 可空6 DataTable dtIdentity = tableInfo.dsTableInfo.Tables[2]; //列名0 seed 1 increment 2 string[] keyColumns = tableInfo.KeyColumns; //主键列 DataTable dtColumnsDescription = pt.GetTableColumnDecription(tableName); //列名0 说明1 //默认值 DataTable dtConstraint = null;//constraint_type(DEFAULT) constraint_keys默认值 //表关系 DataTable dtRelation = null; if (tableInfo.dsTableInfo.Tables.Count > 6) { dtConstraint = tableInfo.dsTableInfo.Tables[6]; //constraint_type(DEFAULT) constraint_keys默认值 dtRelation = tableInfo.dsTableInfo.Tables[6]; //column 0 } word.WriteText("表名:" + tableName + " [" + tableInfo.tableDescription + "]"); int rows = dtColumnType.Rows.Count; word.AddParagraph(); word.AddTable(rows + 1, 7); #region 添加列头 word.AddTableContent(1, 1, "列名", 1, Microsoft.Office.Interop.Word.WdColor.wdColorBlack); word.AddTableContent(1, 2, "类型(长度)", 1, Microsoft.Office.Interop.Word.WdColor.wdColorBlack); word.AddTableContent(1, 3, "可空", 1, Microsoft.Office.Interop.Word.WdColor.wdColorBlack); word.AddTableContent(1, 4, "默认值", 1, Microsoft.Office.Interop.Word.WdColor.wdColorBlack); word.AddTableContent(1, 5, "标识", 1, Microsoft.Office.Interop.Word.WdColor.wdColorBlack); word.AddTableContent(1, 6, "主键", 1, Microsoft.Office.Interop.Word.WdColor.wdColorBlack); word.AddTableContent(1, 7, "列说明", 1, Microsoft.Office.Interop.Word.WdColor.wdColorBlack); #endregion ProcString ps = new ProcString(); string strColName = ""; string strIdentity = ""; string strPrimary = ""; string strDefault = ""; string strColDescription = ""; string tem = ""; int j = 1; for (int i = 0; i < rows; i++) { j++; //列名 strColName = dtColumnType.Rows[i][0].ToString(); word.AddTableContent(j, 1, strColName); //列类型(长度) word.AddTableContent(j, 2, dtColumnType.Rows[i][1].ToString() + "(" + dtColumnType.Rows[i][3].ToString() + ")"); //可空 word.AddTableContent(j, 3, dtColumnType.Rows[i][6].ToString()); //默认值 #region 默认值 if (null != dtConstraint && dtConstraint.Rows.Count > 0) { foreach (DataRow dr in dtConstraint.Rows) { tem = dr[0].ToString(); if (tem.IndexOf("DEFAULT") >= 0) { tem = tem.Substring(18); if (tem == strColName) { strDefault = dr["constraint_keys"].ToString(); break; } } strDefault = ""; } } #endregion word.AddTableContent(j, 4, strDefault); //标识 #region 标识 if (null != dtIdentity && dtIdentity.Rows.Count > 0) { foreach (DataRow dr in dtIdentity.Rows) { if (strColName == dr[0].ToString()) { strIdentity = "yes(" + dr[1].ToString() + "," + dr[2].ToString() + ")"; break; } strIdentity = ""; } } #endregion word.AddTableContent(j, 5, strIdentity); //主键 strPrimary = ps.IsContains(strColName, tableInfo.KeyColumns) ? "yes" : ""; word.AddTableContent(j, 6, strPrimary); //列说明 #region 列说明 if (null != dtColumnsDescription && dtColumnsDescription.Rows.Count > 0) { foreach (DataRow dr in dtColumnsDescription.Rows) { if (strColName == dr[0].ToString()) { strColDescription = dr[1].ToString(); break; } strColDescription = ""; } } #endregion word.AddTableContent(j, 7, strColDescription); } #region 表关系 if (dtRelation.Rows.Count > 0) { tem = ""; foreach (DataRow dr in dtRelation.Rows) { if (dr["status_enabled"].ToString() == "Enabled") { tem += dr["constraint_name"].ToString() + "[" + dr["constraint_keys"].ToString() + "]" + " , "; } } if (tem.EndsWith(" , ")) { tem = tem.Substring(0, tem.Length - 3); word.WriteText("表关系:" + tem); } } #endregion word.AddParagraph(); word.AddParagraph(); }//end foreach word.SaveAs(); }
/// <summary> /// 单表时,生成实例类文件 /// </summary> /// <param name="tableName">表名</param> /// <returns></returns> public string ToModel(bool confirmWrite, string tableName) { StringBuilder ret = new StringBuilder(); ProcTable pt = new ProcTable(); ProcString ps = new ProcString(); ps.SetTabs(strNameSpace); DataTable dtColumnDescription = pt.GetTableColumnDecription(tableName); string strTableDescription = pt.GetTableDesciption(tableName); DataTable dtColumnType = pt.GetTableInfo(tableName).Tables[1]; string strClassName = ps.GetClassNameByTableName(tableName, strPrefix); StringBuilder sbBody = new StringBuilder(); TableInfo tableInfo = new TableInfo(strNameSpace, strPrefix, tableName); foreach (DataRow drType in dtColumnDescription.Rows) { string columnName = drType["ColumnName"].ToString(); //列名 string columnType = drType["ColumnDescription"].ToString(); //列数据类型 string firstLowerColumnName = ""; //首字母小写,用于字段 string firstUpperColumnName = ""; //首字母大写,用于属性 #region 以下为字段 foreach (DataRow drDescription in dtColumnDescription.Rows) { if (drDescription["ColumnName"].ToString() == columnName) { string strColumnDescription = drDescription["ColumnDescription"].ToString(); sbBody.AppendLine(ps.tabMember + "/// <summary>"); sbBody.AppendLine(ps.tabMember + "/// " + strColumnDescription); sbBody.AppendLine(ps.tabMember + "/// <summary>"); } } firstLowerColumnName = ps.ConvertStringToUpperOrLower(columnName, false); //if (!string.IsNullOrEmpty(columnDefault)) //{ // if (columnName.ToUpper() != "HOSPITAL_NO") // { // sbBody.AppendLine(ps.tabMember + "private " + ps.ConvertType(columnType, columnName, tableInfo) + " " + firstLowerColumnName + "=\"" + columnDefault + "\";"); // } //} //else //{ sbBody.AppendLine(ps.tabMember + "private " + ps.ConvertType(columnType, columnName, tableInfo) + " " + firstLowerColumnName + ";"); //} #endregion #region 以下为属性 foreach (DataRow drDescription in dtColumnDescription.Rows) { if (drDescription["ColumnName"].ToString() == columnName) { string strColumnDescription = drDescription["ColumnDescription"].ToString(); sbBody.AppendLine(ps.tabMember + "/// <summary>"); sbBody.AppendLine(ps.tabMember + "/// " + strColumnDescription); sbBody.AppendLine(ps.tabMember + "/// <summary>"); } } firstUpperColumnName = ps.ConvertStringToUpperOrLower(columnName, true); sbBody.AppendLine(ps.tabMember + "public " + ps.ConvertType(columnType, columnName, tableInfo) + " " + firstUpperColumnName); sbBody.AppendLine(ps.tabMember + "{"); sbBody.AppendLine(ps.tabLocalVar + "get{ return " + firstLowerColumnName + "; }"); sbBody.AppendLine(ps.tabLocalVar + "set{ " + firstLowerColumnName + " = value; }"); sbBody.AppendLine(ps.tabMember + "}"); #endregion sbBody.AppendLine(); } string strClass = ps.GetCLassByModel(strNameSpace, strPrefix, tableName, sbBody.ToString()); ret.Append(strClass); #region 写文件 if (confirmWrite) { string tem = path + "/Model/"; if (!Directory.Exists(tem)) { Directory.CreateDirectory(tem); } string fullpath = tem + strClassName + ".cs"; Write(fullpath, ret.ToString()); } #endregion return(ret.ToString()); }