• SQL Table 自动生成Net底层-生成数据操作Repository


    生成接口类

            public static string DataTableToBaseIService(string tableName, string nameSpace, string className)
            {
                var table = SqlTableHelper.GetSQLTableInfo(tableName);
                StringBuilder reval = new StringBuilder();
                reval.AppendFormat(@"       
    using System;
    using System.Collections.Generic;
    using System.Data;
    using {0}.Mapping;
    
    namespace {0}.IService
    {{
        public partial interface {1}
        {{     
            /// <summary>
            /// 保存
            /// </summary>          
            int Save({2} model);
    
            /// <summary>
            /// 获取数据(分页)
            /// </summary>
            /// <param name=""filterSql"">查询条件</param>
            /// <param name=""pageIndex"">页码</param>
            /// <param name=""pageSize"">每页显示数量</param>
            /// <param name=""total"">总行数</param>
            /// <param name=""sort"">排序条件</param>
            /// <returns>返回DataTable</returns>         
            DataTable PageData(string filterSql,int pageIndex,int pageSize,out int total,string sort="""");
    
            /// <summary>
            /// 获取数据(不分页)
            /// </summary>         
            /// <param name=""filterSql"">查询条件</param>
            /// <param name=""sort"">排序条件</param>
            /// <returns>返回DataTable</returns>          
            DataTable PageData(string filterSql,string sort="""");
    
            /// <summary>
            /// 获取单条数据
            /// </summary>
            /// <param name=""filterSql"">查询条件</param>
            /// <returns>返回DataTable</returns>   
            DataTable GetSingle(string filterSql);
    
            /// <summary>
            /// 删除
            /// </summary>          
            int Delete({3} {4});
        }}
    }}", nameSpace, className, tableName, SqlTableToMapping.ChangeType(table.Rows[0]["数据类型"].ToString()), table.Rows[0]["列名"]);
                return reval.ToString();
            }
    
    
            public static string DataTableToUserIService(string nameSpace, string className)
            {
                StringBuilder reval = new StringBuilder();
                reval.AppendFormat(@"       
    using System;
    using System.Collections.Generic;
    using System.Data;
    using {0}.Mapping;
    
    namespace {0}.IService
    {{
        public partial interface {1}
        {{     
        }}
    }}", nameSpace, className);
                return reval.ToString();
            }
    View Code

    实现接口

            #region 生成基础类
            public static string DataTableToBaseRepository(DataTable dt, string className, string nameSpace)
            {
                string key = dt.Rows[0]["列名"].ToString();
                string tableName = dt.Rows[0]["表名"].ToString();
                StringBuilder reval = new StringBuilder();
                reval.AppendFormat(@"       
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Text;
    using System.Data.SqlClient;
    using {0}.Mapping;
    using {0}.IRepository;
    using {0}.Common;
    
    namespace {0}.Repository
    {{
        public partial class {1}:I{1}
        {{
            public int Save({2} model)
            {{
                StringBuilder sb = new StringBuilder();   
                {3}
                {4}         
            }}", nameSpace, className, tableName, GetMaxKey(dt), GetAddMethod(dt, key, tableName));
    
                //生成分页方法
                reval.AppendFormat(@"
            /// <summary>
            /// 获取数据(分页)
            /// </summary>
            /// <param name=""filterSql"">查询条件</param>
            /// <param name=""pageIndex"">页码</param>
            /// <param name=""pageSize"">每页显示数量</param>
            /// <param name=""total"">总行数</param>
            /// <param name=""sort"">排序条件</param>
            /// <returns>返回DataTable</returns>   
            public DataTable PageData(string filterSql, int pageIndex, int pageSize, out int total, string sort = """")
            {{
                {0}
            }}", GetPageMethod(tableName));
    
                reval.AppendFormat(@"
            /// <summary>
            /// 获取数据(分页)
            /// </summary>
            /// <param name=""filterSql"">查询条件</param>
            /// <param name=""sort"">排序条件</param>
            /// <returns>返回DataTable</returns>   
            public DataTable PageData(string filterSql, string sort = """")
            {{
                {0}
            }}", GetListMethod(tableName));
    
                reval.AppendFormat(@"
            /// <summary>
            /// 获取单条数据
            /// </summary>
            /// <param name=""filterSql"">查询条件</param>
            /// <returns>返回DataTable</returns>   
            public DataTable GetSingle(string filterSql)
            {{
                {0}
            }}", GetSingleMethod(tableName));
    
                reval.AppendFormat(@"
            /// <summary>
            /// 删除数据
            /// </summary>
            /// <param name=""key"">主键</param>
            /// <returns>返回DataTable</returns>   
            public int Delete({2} {0})
            {{
                {1}
            }}", key, GetDeleteMethod(tableName, key), SqlTableToMapping.ChangeType(dt.Rows[0]["数据类型"].ToString()));
    
                reval.AppendFormat(@"
        }}
    }}");
                return reval.ToString();
            }
    
            private static string GetMaxKey(DataTable dt)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat(@"sb.AppendFormat("" declare @{0} {1} "");", dt.Rows[0]["列名"], dt.Rows[0]["数据类型"]);
                sb.AppendFormat(@" 
                sb.AppendFormat("" select @{0}=isnull(max({0}),0)+1 from {1}"");", dt.Rows[0]["列名"], dt.Rows[0]["表名"]);
                return sb.ToString();
            }
            private static string GetAddMethod(DataTable dt, string key, string tableName)
            {
                StringBuilder sb = new StringBuilder();
    
                //新增语句
                StringBuilder sbAddMethod = new StringBuilder();
                sbAddMethod.AppendFormat(@"sb.AppendFormat("" insert into {0} (", tableName);
    
                //修改语句
                StringBuilder sbEditMethod = new StringBuilder();
                sbEditMethod.AppendFormat(@"sb.AppendFormat("" set @{0}=model.{0} "");", key);
                sbEditMethod.AppendFormat(@" 
                    sb.AppendFormat("" update {0} set "");", tableName);
    
                StringBuilder sbColumns = new StringBuilder();
                StringBuilder sbColumnValues = new StringBuilder();
    
                //参数
                StringBuilder sbPara = new StringBuilder();
                sbPara.AppendFormat(@" 
                var paras = new List<SqlParameter>();");
    
                for (var i = 0; i < dt.Rows.Count; i++)
                {
                    sbColumns.AppendFormat(" {0},", dt.Rows[i]["列名"]);
                    if (i == 0)
                    {
                        sbColumnValues.AppendFormat(" @{0},", key);
                    }
                    else
                    {
                        sbColumnValues.AppendFormat(" @{0},", dt.Rows[i]["列名"]);
                        sbPara.AppendFormat(@" 
                paras.Add(new SqlParameter(""@{0}"",model.{0}));", dt.Rows[i]["列名"]);
                    }
                    if (i == dt.Rows.Count - 1)
                    {
                        sbEditMethod.AppendFormat(@"  
                    sb.AppendFormat("" @{0}={{0}}"",model.{0});", dt.Rows[i]["列名"]);
                    }
                    else
                    {
                        sbEditMethod.AppendFormat(@"  
                    sb.AppendFormat("" @{0}={{0}},"",model.{0});", dt.Rows[i]["列名"]);
                    }
                }
                sbColumns = new StringBuilder(sbColumns.ToString().Substring(0, sbColumns.Length - 1));
                sbColumns.Append(@""");");
    
                sbAddMethod.Append(sbColumns);
    
                sbAddMethod.AppendFormat(@" 
                    sb.AppendFormat("" select @{0},", key);
                sbColumnValues = new StringBuilder(sbColumnValues.ToString().Substring(0, sbColumnValues.Length - 1));
                sbColumnValues.Append(@""");");
                sbAddMethod.Append(sbColumnValues);
    
                sbEditMethod.AppendFormat(@" 
                    sb.AppendFormat("" where {0}={{0}},"",model.{0});", key);
                sb.AppendFormat(@"
                if(model.{0}==0)
                {{
                    {1}
                }}
                else
                {{
                    {2}
                }}", key, sbAddMethod, sbEditMethod);
    
    
    
                sb.Append(sbPara);
                sb.AppendFormat(@" 
                return DbHelperSQL.ExecuteSql(sb.ToString(),paras.ToArray());");
                return sb.ToString();
            }
    
            private static string GetPageMethod(string tableName)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat(@" 
                string cmdText = ""Proc_UP_Pagination"";
                DataTable dt = null;
                SqlParameter[] paras = new SqlParameter[] {{ 
                    new SqlParameter(""@TableName"",""{0}""),
                    new SqlParameter(""@Fields"",""*""),
                    new SqlParameter(""@OrderField"",sort),
                    new SqlParameter(""@sqlWhere"",filterSql),
                    new SqlParameter(""@pageSize"",pageSize),
                    new SqlParameter(""@pageIndex"",pageIndex),
                    new SqlParameter(""@Total"",SqlDbType.Int,11),
                }};
                paras[6].Direction = ParameterDirection.Output; 
                dt = DbHelperSQL.RunProcedure(cmdText, paras,""{0}"").Tables[0];
                total = Convert.ToInt32(paras[6].Value);
                return dt;", tableName);
    
                return sb.ToString();
            }
            public static string GetListMethod(string tableName)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat(@"
                string cmdText = string.Empty;
                DataTable dt = null;
                
                cmdText = string.Format(""SELECT * FROM {0} WHERE 1=1"");
                if (!string.IsNullOrEmpty(filterSql))
                    cmdText += string.Format(""  AND "" + filterSql);
                if (!string.IsNullOrEmpty(sort))
                    cmdText += string.Format(""  ORDER BY "" + sort);
                dt = DbHelperSQL.Query(cmdText).Tables[0];
                
                return dt;", tableName);
    
                return sb.ToString();
            }
    
            public static string GetSingleMethod(string tableName)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat(@"
                string cmdText = string.Empty;
                DataTable dt = null;
                
                cmdText = string.Format(""SELECT TOP 1 * FROM {0} WHERE 1=1"");
                if (!string.IsNullOrEmpty(filterSql))
                    cmdText += string.Format(""  AND "" + filterSql);
                dt = DbHelperSQL.Query(cmdText).Tables[0];
                
                return dt;", tableName);
    
    
                return sb.ToString();
            }
    
            public static string GetDeleteMethod(string tableName, string key)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat(@"
                string cmdText = string.Empty;
                
                cmdText = string.Format(""DELETE FROM {0} WHERE {1}=@{1}"");
                SqlParameter[] paras = new SqlParameter[] {{ 
                    new SqlParameter(""@{1}"",{1}),
                }};
                var iret = DbHelperSQL.ExecuteSql(cmdText);
                
                return iret;", tableName, key);
    
                return sb.ToString();
            }
            #endregion
    
           #region 生成自定义类
    
            public static string DataTableToUserRepository(string className, string nameSpace)
            {
                StringBuilder reval = new StringBuilder();
                reval.AppendFormat(@"       
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Text;
    using System.Data.SqlClient;
    using {0}.Mapping;
    
    namespace {0}.Repository
    {{
        public partial class {1}
        {{
        }}", nameSpace, className);
                return reval.ToString();
            }
    
            #endregion
    View Code

     生成Autofac注入类

            public static string AutofaceClass(string nameSpace, DataTable dt)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat(@"
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Autofac;
    using {0}.Mapping;
    using {0}.IRepository;
    namespace {0}.Repository
    {{
        public class RepositoryModule : Module
        {{
            protected override void Load(ContainerBuilder builder)
            {{", nameSpace);
    
                for (var i = 0; i < dt.Rows.Count; i++)
                {
                    sb.AppendFormat(@"
                builder.RegisterType<{0}>().As<{1}>();", dt.Rows[i]["name"] + "Repository", "I" + dt.Rows[i]["name"] + "Repository");
                }
    
                sb.Append(@"
                base.Load(builder);
            }
        }
    }");
                return sb.ToString();
    View Code
  • 相关阅读:
    注册表清理bat
    apache2.2.14 负载均衡过程中遇到问题记录;
    thikpad 中eclipse/idea 无法获取鼠标
    PLSQL DEVELOPER 提示(错误)信息是乱码;亲测有效
    windows右键 打开方式 浏览 无法添加默认打开方式 解决
    澳洲值得代购物品汇总
    5 open source dashboard tools for visualizing data
    nginx概述
    Linux bash 参数处理办法
    Linux shell 启动配置文件设置
  • 原文地址:https://www.cnblogs.com/plming/p/8033720.html
Copyright © 2020-2023  润新知