///////////////////////以下生成 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()); }
/// <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()); }