public static string Get(DbTable table, string namespaceName)
        {
            StringBuilder sb      = new StringBuilder();
            List <string> exclude = new List <string> {
                "IsDel", "CreateAt", "CreateBy", "Creator", "UpdateAt", "UpdateBy", "Updator"
            };

            string className = "";

            string[] subNames = table.name.Split('_');
            if (subNames.Length > 0)
            {
                className = subNames[subNames.Length - 1];
            }
            else
            {
                className = table.name;
            }

            // 命名空间
            sb.Append("using HZC.Core;" + Environment.NewLine);
            sb.Append("using HZC.Database;" + Environment.NewLine);
            sb.Append("using System;" + Environment.NewLine);
            sb.Append(Environment.NewLine);
            sb.Append("namespace " + namespaceName + ".Services" + Environment.NewLine);
            sb.Append("{" + Environment.NewLine);

            sb.Append("    [MyDataTable(\"" + table.name + "\")]" + Environment.NewLine);
            sb.Append("    public partial class " + className + "Entity : BaseEntity" + Environment.NewLine);
            sb.Append("    {" + Environment.NewLine);
            string[] ignoreColumns = new string[] { "Id", "CreateAt", "CreateBy", "Creator", "UpdateAt", "UpdateBy", "Updator" };

            for (int i = 0; i < table.columns.Count; i++)
            {
                var dr = table.columns[i];
                if (!ignoreColumns.Contains(dr.name))
                {
                    sb.Append("        /// <summary>" + Environment.NewLine);
                    sb.Append("        /// " + (string.IsNullOrWhiteSpace(dr.description) ? dr.name : dr.description) + Environment.NewLine);
                    sb.Append("        /// </summary>" + Environment.NewLine);
                    sb.Append("        public " + DataColumnConvertor.ToCSharpType(dr) + " " + dr.name + " { get; set; }" + Environment.NewLine);
                    sb.Append(Environment.NewLine);
                }
            }
            sb.Append("    }" + Environment.NewLine);

            sb.Append("}" + Environment.NewLine);
            return(sb.ToString());
        }
        public static string Get(DbTable table, string ns)
        {
            StringBuilder sb    = new StringBuilder();
            int           total = table.columns.Count;

            // 命名空间
            sb.Append("using System;" + Environment.NewLine);
            sb.Append("using System.Collections.Generic;" + Environment.NewLine);
            sb.Append("using System.Configuration;" + Environment.NewLine);
            sb.Append("using System.Data;" + Environment.NewLine);
            sb.Append("using System.Data.SqlClient;" + Environment.NewLine);
            sb.Append("using " + ns + ".Core;" + Environment.NewLine);

            // 开始定义类
            sb.Append("namespace " + ns + ".Services" + Environment.NewLine);
            sb.Append("{" + Environment.NewLine);
            sb.Append("    public partial class " + table.name + "Factory : BaseFactory<" + table.name + ">" + Environment.NewLine);
            sb.Append("    {" + Environment.NewLine);
            sb.Append("        #region 构造函数" + Environment.NewLine);
            sb.Append("        public " + table.name + "Factory() : base() { }" + Environment.NewLine);
            sb.Append("        public " + table.name + "Factory(string connSettingKeyName) : base(connSettingKeyName) { }" + Environment.NewLine);
            sb.Append("        #endregion" + Environment.NewLine);
            sb.Append(Environment.NewLine);

            sb.Append("        #region 增改数据" + Environment.NewLine);
            sb.Append("        public override int Create(" + table.name + " model, int operatorId)" + Environment.NewLine);
            sb.Append("        {" + Environment.NewLine);
            sb.Append("            try" + Environment.NewLine);
            sb.Append("            {" + Environment.NewLine);
            sb.Append("                using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings[_connectionString]))" + Environment.NewLine);
            sb.Append("                {" + Environment.NewLine);
            sb.Append("                    SqlParameter[] param = {" + Environment.NewLine);

            for (int i = 0; i < total; i++)
            {
                var dr = table.columns[i];
                if (dr.name != "IsDel" && dr.name != "CreateAt" && dr.name != "UpdateAt" && dr.name != "UpdateBy")
                {
                    sb.Append("                        new SqlParameter(\"@" + dr.name + "\", " + DataColumnConvertor.ToSqlDbType(dr) + ")," + Environment.NewLine);
                }
            }

            sb.Append("                    };" + Environment.NewLine);

            sb.Append(Environment.NewLine);

            sb.Append("                    param[0].Direction = ParameterDirection.Output;" + Environment.NewLine);
            var j = 0;

            for (int i = 0; i < total; i++)
            {
                var dr = table.columns[i];
                if (dr.name != "IsDel" && dr.name != "CreateAt" && dr.name != "UpdateAt" && dr.name != "UpdateBy" && dr.name != table.name + "Id")
                {
                    if (dr.name == "CreateBy")
                    {
                        sb.Append("                    param[" + (j + 1) + "].Value = operatorId;" + Environment.NewLine);
                    }
                    else
                    {
                        sb.Append("                    param[" + (j + 1) + "].Value = model." + dr.name + ";" + Environment.NewLine);
                    }
                    j++;
                }
            }
            sb.Append("                    int obj = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, \"" + table.name + "_Add\", param);" + Environment.NewLine);
            sb.Append(Environment.NewLine);
            sb.Append("                    return obj == 0 ? 0 : Convert.ToInt32(param[0].Value);" + Environment.NewLine);
            sb.Append("                }" + Environment.NewLine);
            sb.Append("            }" + Environment.NewLine);
            sb.Append("            catch(Exception ex)" + Environment.NewLine);
            sb.Append("            {" + Environment.NewLine);
            sb.Append("                DbExceptionHelper.DoException(\"" + table.name + "Factory.Create\", ex);" + Environment.NewLine);
            sb.Append("                return 0;" + Environment.NewLine);
            sb.Append("            }" + Environment.NewLine);
            sb.Append("        }" + Environment.NewLine);
            sb.Append(Environment.NewLine);

            sb.Append("        public override int Update(" + table.name + " model, int operatorId)" + Environment.NewLine);
            sb.Append("        {" + Environment.NewLine);
            sb.Append("            try" + Environment.NewLine);
            sb.Append("            {" + Environment.NewLine);
            sb.Append("                using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings[_connectionString]))" + Environment.NewLine);
            sb.Append("                {" + Environment.NewLine);
            sb.Append("                    SqlParameter[] param = {" + Environment.NewLine);


            for (int i = 0; i < total; i++)
            {
                var dr = table.columns[i];
                if (dr.name != "IsDel" && dr.name != "CreateAt" && dr.name != "UpdateAt" && dr.name != "CreateBy")
                {
                    sb.Append("                        new SqlParameter(\"@" + dr.name + "\", " + DataColumnConvertor.ToSqlDbType(dr) + ")," + Environment.NewLine);
                }
            }

            sb.Append("                    };" + Environment.NewLine);

            sb.Append(Environment.NewLine);
            j = 0;
            for (int i = 0; i < total; i++)
            {
                var dr = table.columns[i];
                if (dr.name != "IsDel" && dr.name != "CreateAt" && dr.name != "UpdateAt" && dr.name != "CreateBy")
                {
                    if (dr.name == "UpdateBy")
                    {
                        sb.Append("                    param[" + j + "].Value = operatorId;" + Environment.NewLine);
                    }
                    else
                    {
                        sb.Append("                    param[" + j + "].Value = model." + dr.name + ";" + Environment.NewLine);
                    }
                    j++;
                }
            }
            sb.Append(Environment.NewLine);
            sb.Append("                    return SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, \"" + table.name + "_Update\", param);" + Environment.NewLine);
            sb.Append("                }" + Environment.NewLine);
            sb.Append("            }" + Environment.NewLine);
            sb.Append("            catch(Exception ex)" + Environment.NewLine);
            sb.Append("            {" + Environment.NewLine);
            sb.Append("                DbExceptionHelper.DoException(\"" + table.name + "Factory.Update\", ex);" + Environment.NewLine);
            sb.Append("                return 0;" + Environment.NewLine);
            sb.Append("            }" + Environment.NewLine);
            sb.Append("        }" + Environment.NewLine);
            sb.Append("        #endregion" + Environment.NewLine);
            sb.Append("    }" + Environment.NewLine);
            sb.Append("}" + Environment.NewLine);

            return(sb.ToString());
        }
        public static string Get(DbTable table)
        {
            StringBuilder sb = new StringBuilder();

            List <string> addExclude = new List <string> {
                "CreateAt", "UpdateAt", "UpdateBy", table.name + "Id", "IsDel"
            };
            List <DbTableColumn> addColumns = table.columns.Where(c => !addExclude.Contains(c.name)).ToList();

            List <string> editExclude = new List <string> {
                "CreateAt", "CreateBy", "UpdateAt", "IsDel"
            };
            List <DbTableColumn> editColumns = table.columns.Where(c => !editExclude.Contains(c.name)).ToList();

            // 创建
            sb.Append("if exists (select * from dbo.sysobjects where id = object_id(N'[" + table.name + "_Add]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)" + Environment.NewLine);
            sb.Append("drop procedure [" + table.name + "_Add]" + Environment.NewLine);
            sb.Append("GO" + Environment.NewLine);
            sb.Append("--------------------------------------------------" + Environment.NewLine);
            sb.Append("--用途:增加一条记录 " + Environment.NewLine);
            sb.Append("--项目名称:" + _namespace + Environment.NewLine);
            sb.Append("--说明:自动生成" + Environment.NewLine);
            sb.Append("--时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine);
            sb.Append("--------------------------------------------------" + Environment.NewLine);
            sb.Append(Environment.NewLine);
            sb.Append("CREATE PROCEDURE [dbo].[" + table.name + "_Add]" + Environment.NewLine);
            sb.Append("    @" + table.name + "Id int output," + Environment.NewLine);

            for (int i = 0; i < addColumns.Count; i++)
            {
                var dr = addColumns[i];
                sb.Append("    @" + dr.name + " " + DataColumnConvertor.ToProcType(dr) + (i == (addColumns.Count - 1) ? "" : ",") + Environment.NewLine);
            }

            sb.Append(Environment.NewLine);
            sb.Append("AS" + Environment.NewLine);
            sb.Append(Environment.NewLine);

            sb.Append("INSERT INTO [" + table.name + "] ( ");

            for (int i = 0; i < addColumns.Count; i++)
            {
                var dr = addColumns[i];
                sb.Append("[" + dr.name + "],");
            }

            sb.Append("IsDel,CreateAt,UpdateBy,UpdateAt) " + Environment.NewLine);
            sb.Append("    VALUES ( ");
            for (int i = 0; i < addColumns.Count; i++)
            {
                var dr = addColumns[i];
                sb.Append("@" + dr.name + ",");
            }
            sb.Append("0,GETDATE(),@CreateBy,GETDATE() );" + Environment.NewLine);
            sb.Append(Environment.NewLine);
            sb.Append("SET @" + table.name + "Id = @@IDENTITY;" + Environment.NewLine);
            sb.Append("GO" + Environment.NewLine);
            sb.Append(Environment.NewLine);

            // 修改
            sb.Append("if exists (select * from dbo.sysobjects where id = object_id(N'[" + table.name + "_Update]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)" + Environment.NewLine);
            sb.Append("drop procedure [" + table.name + "_Add]" + Environment.NewLine);
            sb.Append("GO" + Environment.NewLine);
            sb.Append("--------------------------------------------------" + Environment.NewLine);
            sb.Append("--用途:修改一条记录 " + Environment.NewLine);
            sb.Append("--项目名称:" + _namespace + Environment.NewLine);
            sb.Append("--说明:自动生成" + Environment.NewLine);
            sb.Append("--时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine);
            sb.Append("--------------------------------------------------" + Environment.NewLine);
            sb.Append(Environment.NewLine);
            sb.Append("CREATE PROCEDURE [dbo].[" + table.name + "_Update]" + Environment.NewLine);

            for (int i = 0; i < editColumns.Count; i++)
            {
                var dr = editColumns[i];
                sb.Append("    @" + dr.name + " " + DataColumnConvertor.ToProcType(dr) + (i == (editColumns.Count - 1) ? "" : ",") + Environment.NewLine);
            }

            sb.Append(Environment.NewLine);
            sb.Append("AS" + Environment.NewLine);
            sb.Append(Environment.NewLine);

            sb.Append("UPDATE [" + table.name + "] SET ");

            for (int i = 0; i < editColumns.Count; i++)
            {
                var dr = editColumns[i];
                if (dr.name != table.name + "Id")
                {
                    sb.Append("[" + dr.name + "]=@" + dr.name + ",");
                }
            }

            sb.Append("UpdateAt=GETDATE() WHERE " + table.name + "Id = @" + table.name + "Id;" + Environment.NewLine);
            sb.Append("GO" + Environment.NewLine);

            return(sb.ToString());
        }