Exemplo n.º 1
0
        /// <summary>
        /// 根据数据表获取列,并且输出实体类文件
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private string GetColumnByTable(HttpContext context)
        {
            try
            {
                crudHelper = new CRUDHelper <object>(HttpContext.Current.Session["strConnection"]?.ToString(), HttpContext.Current.Session["strType"]?.ToString());
                string table       = context.Request.Params["table"];
                Array  arr         = table.Split(',');
                string currentPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "WebForms\\CreateModel\\ModelFile";
                //清除文件夹中所有文件
                if (!FolderHelper.IsEmptyDirectory(currentPath))
                {
                    FolderHelper.DeleteFolder(currentPath, false);
                }

                foreach (string item in arr)
                {
                    string        sql        = $"select t.column_name names,t.comments,u.DATA_TYPE types from user_col_comments t join user_tab_cols u on t.column_name=u.COLUMN_NAME where t.table_name='{item}' and u.TABLE_NAME='{item}' order by u.INTERNAL_COLUMN_ID";
                    DataTable     data       = crudHelper.SelectBySQL(sql);
                    string        className  = item.ToUpper();
                    StringBuilder classStr   = new StringBuilder();
                    bool          isIdentity = true;
                    classStr.Append(" using DotNet.Utils;\n");
                    classStr.Append(" using System;\n");
                    classStr.Append(" namespace Models\n{\n");
                    classStr.Append("  /// <summary>\n/// " + item + "\n/// </summary>\n");
                    classStr.Append("  public  class " + className + "\n{\n");

                    foreach (DataRow dr in data?.Rows)
                    {
                        TempTable tt = new TempTable()
                        {
                            names    = dr["NAMES"].ToString(),
                            types    = dr["TYPES"].ToString(),
                            comments = dr["COMMENTS"].ToString()
                        };
                        classStr.Append("  /// <summary>\n/// " + tt.comments + "\n/// </summary>\n");
                        if (isIdentity)
                        {
                            classStr.Append("[Column(Identity =true)]\n");
                            isIdentity = false;
                        }
                        classStr.Append("  public  " + GetModelType(tt.types) + tt.names + " { get; set; }\n\n");
                    }
                    classStr.Append(" }\n");
                    classStr.Append("public class " + className + "Manage : CRUDHelper<" + className + ">\n{ \n}\n");
                    classStr.Append(" }\n");
                    string path = currentPath + "\\" + className + ".cs";
                    FileHelper.Write(path, classStr.ToString());
                }
                string err  = "";
                bool   bZip = ZipHelper.ZipFile(currentPath, currentPath + "\\Model.Zip", out err);
                if (bZip)
                {
                    return("1");
                }
                else
                {
                    return(err);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex);
                return(ex.Message);
            }
        }