/// <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()); }
/// <summary> /// 多表时,生成单表实体字符串 /// </summary> /// <param name="dsColumnType">列类型表</param> /// <param name="dtColumnDescription">列描述表</param> /// <param name="dtDescription">表描述(表)</param> /// <returns></returns> public string GetTable(DataTable dtColumnType , DataTable dtColumnDescription , string strTableDescription , string strPrefix , string strNamespace) { StringBuilder ret = new StringBuilder(); string strTableName = dtColumnType.TableName; //lsj--构造方法1所用的参数默认值 string strDefaultValue = ""; //lsj--构造方法2所用的参数列表 string strPara = ""; //lsj--构造方法3的赋值 int index = 0; string strIndex = ""; string strValue = ""; //lsj--构造方法1所用的赋值语句。 string[] strSetValues1 = new string[dtColumnType.Rows.Count]; //lsj--构造方法2所用的赋值语句。 string[] strSetValues2 = new string[dtColumnType.Rows.Count]; ProcString ps = new ProcString(); ps.SetTabs(strNamespace); //lsj--类名 string strClassName = ps.GetClassName(strTableName);//2021-2-2-lsj //lsj--小写的类名--在此做类的对象来用。 string strLowerClassName = ps.ConvertStringToUpperOrLower(strClassName, false); TableInfo tableInfo = new TableInfo(strNamespace, strPrefix, strTableName); if (string.IsNullOrEmpty(tableInfo.strPKName)) { return(""); } //--------------------------------------------- #region Member Variables / Attributes 属性 StringBuilder sbBody = new StringBuilder(); sbBody.AppendLine(ps.tabMember + "#region Member Variables"); sbBody.AppendLine(ps.tabMember + "SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();//雪花ID"); for (int i = 0; i < dtColumnType.Rows.Count; i++) { DataRow drType = dtColumnType.Rows[i]; string columnName = drType["Column_name"].ToString(); //列名 string columnType = drType["Type"].ToString(); //列数据类型 //string columnDefault = drType["Data_default"].ToString().Trim();//列默认值//增加默认值 lsj@20130222--不支持SYSTIMESTAMP等系统函数 //2021-2-2-lsj--未找到默认值 string columnDefault = ""; string firstLowerColumnName = "";//首字母小写,用于字段 #region 以下为字段 firstLowerColumnName = ps.ConvertStringToUpperOrLower(columnName, false); //if (tableInfo.KeyColumns.Length>1 && tableInfo.KeyColumns[0] == columnName && tableInfo.KeyType == "NUMBER") // sbBody.AppendLine(ps.tabMember + "private Int64 " + firstLowerColumnName + ";"); //else //{ // sbBody.AppendLine(ps.tabMember + "private " + ps.ConvertType(columnType, columnName, tableInfo) + " " + firstLowerColumnName + ";"); //} //列默认值//增加默认值 lsj@20130222--不支持SYSTIMESTAMP等系统函数 sbBody.AppendLine(ps.tabMember + "private " + ps.ConvertType(columnType, columnName, tableInfo) + " " + firstLowerColumnName + ";"); #endregion } sbBody.AppendLine(ps.tabMember + "#endregion"); sbBody.AppendLine(ps.tabMember + "#region Attributes"); for (int n = 0; n < dtColumnType.Rows.Count; n++) { DataRow drType = dtColumnType.Rows[n]; string columnName = drType["Column_name"].ToString(); //列名 string columnType = drType["Type"].ToString(); //列数据类型 //string columnDefault = drType["Data_default"].ToString().Trim();//列默认值//增加默认值 lsj@20130222--不支持SYSTIMESTAMP等系统函数 //2021-2-2-lsj--未找到默认值 string columnDefault = ""; string firstLowerColumnName = ""; //首字母小写,用于字段 string firstUpperColumnName = ""; //首字母大写,用于属性 string strColumnDescription = ""; //列描述 firstLowerColumnName = ps.ConvertStringToUpperOrLower(columnName, false); firstUpperColumnName = ps.ConvertStringToUpperOrLower(columnName, true); if (null != dtColumnDescription) { for (int i = 0; i < dtColumnDescription.Rows.Count; i++) { string strColumnName = dtColumnDescription.Rows[i]["ColumnName"].ToString(); if (strColumnName == columnName) { strColumnDescription = dtColumnDescription.Rows[i]["ColumnDescription"].ToString(); dtColumnDescription.Rows.RemoveAt(i); break; } } } #region 以下为属性 if (!string.IsNullOrEmpty(strColumnDescription)) { sbBody.AppendLine(ps.tabMember + "/// <summary>"); sbBody.AppendLine(ps.tabMember + "/// " + strColumnDescription); sbBody.AppendLine(ps.tabMember + "/// <summary>"); } //sbBody.AppendLine(ps.tabMember + "[DataMember]");//lsj-2021-2-2 firstUpperColumnName = ps.ConvertStringToUpperOrLower(columnName, true); firstUpperColumnName = ps.ConvertToSpecial(firstUpperColumnName, strTableName); //if (n == 0 && tableInfo.KeyColumns[0] == columnName && tableInfo.KeyType == "NUMBER") // sbBody.AppendLine(ps.tabMember + "public Int64 " + firstUpperColumnName); //else //{ sbBody.AppendLine(ps.tabMember + "public " + ps.ConvertType(columnType, columnName, tableInfo) + " " + firstUpperColumnName); //} sbBody.AppendLine(ps.tabMember + "{"); sbBody.AppendLine(ps.tabLocalVar + "get{ return " + firstLowerColumnName + "; }"); sbBody.Append(ps.tabLocalVar + "set{ " + firstLowerColumnName + " = value; "); sbBody.AppendLine("}"); sbBody.AppendLine(ps.tabMember + "}"); //---------------------------------------------- strSetValues1[index] = ps.tabLocalVar + "this." + firstLowerColumnName + " = " + firstLowerColumnName + ";//" + strColumnDescription; strSetValues2[index] = ps.tabLocalVar + "this." + firstLowerColumnName + " = " + strLowerClassName + "." + firstLowerColumnName + ";//" + strColumnDescription; if (index == 0) { //lsj--构造方法1所用的参数默认值 //增加默认值 lsj@20130222--不支持SYSTIMESTAMP等系统函数 strDefaultValue += ps.ConvertDefaultValue(columnType, columnName, columnDefault); //lsj--构造方法2所用的参数列表 //if (n == 0 && tableInfo.KeyColumns[0] == columnName && tableInfo.KeyType == "NUMBER") // strPara += " Int64 " + firstLowerColumnName; //else strPara += ps.ConvertType(columnType, columnName, tableInfo) + " " + firstLowerColumnName; //lsj--构造方法3的赋值 strIndex += "{" + index.ToString() + "}"; strValue += "this." + firstLowerColumnName; } else//前面加, { //lsj--构造方法1所用的参数默认值 if (columnName == "") { strDefaultValue += ",0"; } else { strDefaultValue += "," + ps.ConvertDefaultValue(columnType, columnName, columnDefault); } //lsj--构造方法2所用的参数列表 strPara += "," + ps.ConvertType(columnType, columnName, tableInfo) + " " + firstLowerColumnName; //lsj--构造方法3的赋值 strIndex += "," + "{" + index.ToString() + "}"; strValue += ",this." + firstLowerColumnName; } //\"{0},{1},{2},{3}\", this.id, this.code, this.name, this.frequency index++; #endregion } sbBody.AppendLine(ps.tabMember + "#endregion "); #endregion #region Constructors--构造方法//lsj sbBody.AppendLine(ps.tabMember + "#region Constructors"); //构造方法1 sbBody.AppendLine(ps.tabMember + "public " + strClassName + " (): this(" + strDefaultValue + "){}"); //构造方法2 sbBody.AppendLine(ps.tabMember + "public " + strClassName + " (" + strPara + ")"); sbBody.AppendLine(ps.tabMember + "{"); foreach (string setValue in strSetValues1) { sbBody.AppendLine(setValue); } sbBody.AppendLine(ps.tabMember + "}"); //构造方法3 sbBody.AppendLine(ps.tabMember + "public " + strClassName + " (" + strClassName + " " + strLowerClassName + ")"); sbBody.AppendLine(ps.tabMember + "{"); foreach (string setValue in strSetValues2) { sbBody.AppendLine(setValue); } sbBody.AppendLine(ps.tabMember + "}"); sbBody.AppendLine(ps.tabMember + "#endregion "); #endregion sbBody.Append(ps.GetDataAccessClassBody_SQL(strTableName, strPrefix, strNamespace)); //2021-2-10-lsj-写入数据库 sbBody.Append(ps.GetDataAccessEntity(strTableName, strPrefix, strNamespace)); string strClass = ps.GetCLassByModel(strNamespace, strPrefix, strTableName, sbBody.ToString()); ret.Append(strClass); return(ret.ToString()); }