首页 > 代码库 > 分享最近抽空写的一个代码生成器,集成EasyDBUtility数据库访问帮助类

分享最近抽空写的一个代码生成器,集成EasyDBUtility数据库访问帮助类

一直想写一个自己的代码生成器,但是因为工作事情多,一直搁置下来,最近下决心终于利用下班时间写完了,现在分享给有需要的朋友,代码生成器集成EasyDBUtility数据库访问帮助类,暂时只支持sqlserver数据库,界面如下

部分代码如下

using System;using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using System.Linq;using System.Text;using EasyDBUtility;namespace CodeGenerator{    /// <summary>    /// 生成sqlserver相关代码    /// </summary>    public class SqlServer    {        //获取所有数据库名称        public const string Get_DataBaseName_SQL = "SELECT Name FROM Master..SysDatabases where Name not in(‘master‘,‘model‘,‘msdb‘,‘tempdb‘) ORDER BY Name";        //获取指定数据库中所有表名        public const string Get_Tables_SQL = "SELECT Name FROM [{0}]..SysObjects Where XType=‘U‘ ORDER BY Name";        //获取指定数据库中所有视图        public const string Get_Views_SQL = "SELECT Name FROM [{0}]..SysObjects Where XType=‘V‘ ORDER BY Name";        //获取指定数据库中所有存储过程        public const string Get_Procedures_SQL = "SELECT Name FROM [{0}]..SysObjects Where XType=‘P‘ ORDER BY Name";        //获取指定表或视图中的所有列信息        public const string Get_Columns_SQL = "select a.name,b.name typeName,a.is_identity,a.is_nullable from sys.columns a " +                    "left join sys.types b on a.user_type_id=b.user_type_id where a.object_id=object_id(‘{0}‘) order by column_id";        //获取指定表中主键信息        public const string GetPKeys_SQL = "Declare @objectid int;Set @objectid=object_id(‘{0}‘);Select col_name(@objectid,colid) " +            "From sysobjects as o Inner Join sysindexes as i On i.name=o.name Inner Join sysindexkeys as k On k.indid=i.indid " +            "Where o.xtype = ‘PK‘ and parent_obj=@objectid and k.id=@objectid";        //获取指定列的数据类型        public const string Get_Type_Name_SQL = "SELECT type_name(user_type_id) as [type_name] FROM sys.columns where [object_id] = object_id(‘{0}‘) and [name] = ‘{1}‘";        /// <summary>        /// 生成业务逻辑层代码        /// </summary>        /// <param name="tableName">表名</param>        /// <param name="connectionString">数据库连接字符串</param>        /// <param name="paramsData">生成参数</param>        /// <returns></returns>        public static string CreateBLL(string tableName, string connectionString, ParamsData paramsData)        {            string primaryKeyName = string.Empty;//主键名称            string primaryKeyTypeName = string.Empty;//主键数据类型            SqlHelper helper = new SqlHelper(connectionString);            helper.AutoClose = false;            //获取表的主键列名称            helper.CreateCommand(string.Format(GetPKeys_SQL, tableName));            object primaryKeyObj = helper.ExecuteScalar();            if (primaryKeyObj != null)            {                primaryKeyName = primaryKeyObj.ToString();                helper.CommandText(string.Format(Get_Type_Name_SQL, tableName, primaryKeyName));                primaryKeyTypeName = helper.ExecuteScalar().ToString();            }            else            {                //获取表的所有列信息                helper.CommandText(string.Format(SqlServer.Get_Columns_SQL, tableName));                List<ColumnsData> columnList = helper.ExecuteReader<ColumnsData>();                ColumnsData columnsData = (from col in columnList where col.Is_identity == true select col).FirstOrDefault();                if (columnsData != null)                {                    primaryKeyName = columnsData.Name;                    primaryKeyTypeName = columnsData.TypeName;                }            }            helper.Close();            if (string.IsNullOrEmpty(primaryKeyName) || string.IsNullOrEmpty(primaryKeyTypeName))            {                throw new Exception("在表“" + tableName + "”中没有找到主键或自增列,无法生成BLL代码!请设置表的主键或自增列后再重试!");            }            string deleteThePrefixTheTableName = frmMain.GetDeleteThePrefixTheTableName(tableName, paramsData);//生成的类名                        string parameterName = primaryKeyName.Substring(0, 1).ToLower() + primaryKeyName.Substring(1);            StringBuilder bll = new StringBuilder();            bll.Append("using System.Collections.Generic;\r\n");            if (paramsData.Architecture == Architecture.反射工厂模式)                bll.Append("using DALFactory;\r\n");            bll.Append("using " + paramsData.IDALNamespace + "\r\n");            bll.Append("using " + paramsData.ModelNamespace + "\r\n\r\n");            bll.Append("namespace " + paramsData.BLLNamespace + "\r\n");            bll.Append("{\r\n    ");            bll.Append("public class " + deleteThePrefixTheTableName + paramsData.BLLSuffix + "\r\n    ");            bll.Append("{\r\n        ");            if (paramsData.Architecture == Architecture.反射工厂模式)            {                bll.Append("private static readonly I" + deleteThePrefixTheTableName + " dal = DataAccess.CreateInstance<I" + deleteThePrefixTheTableName + ">(\""                    + deleteThePrefixTheTableName + paramsData.DALSuffix + "\");\r\n\r\n");            }            else if (paramsData.Architecture == Architecture.简单三层)            {                bll.Append("private static readonly " + deleteThePrefixTheTableName + paramsData.DALSuffix + " dal = new " + deleteThePrefixTheTableName + paramsData.DALSuffix + "();\r\n\r\n");            }            //新增方法            if (paramsData.CreateInsertMethod)            {                bll.Append("        public static bool " + paramsData.InsertMethodName + "(" + deleteThePrefixTheTableName + paramsData.ModelSuffix + " data)\r\n        ");                bll.Append("{\r\n            ");                bll.Append("return dal." + paramsData.InsertMethodName + "(data);\r\n        ");                bll.Append("}\r\n\r\n");            }            //删除            if (paramsData.CreateDeleteMethod)            {                bll.Append("        public static bool " + paramsData.DeleteMethodName + "(" + ChangeToCSharpType(primaryKeyTypeName, false) + " " + parameterName + ")\r\n        ");                bll.Append("{\r\n            ");                bll.Append("return dal." + paramsData.DeleteMethodName + "(" + parameterName + ");\r\n        ");                bll.Append("}\r\n\r\n");            }            //批量删除            if (paramsData.CreateBatchDeleteMethod)            {                bll.Append("        public static bool " + paramsData.BatchDeleteMethodName + "(" + ChangeToCSharpType(primaryKeyTypeName, false) + "[] " + parameterName + ")\r\n        ");                bll.Append("{\r\n            ");                bll.Append("return dal." + paramsData.BatchDeleteMethodName + "(" + parameterName + ");\r\n        ");                bll.Append("}\r\n\r\n");            }            //修改            if (paramsData.CreateUpdateMethod)            {                bll.Append("        public static bool " + paramsData.UpdateMethodName + "(" + deleteThePrefixTheTableName + paramsData.ModelSuffix + " data)\r\n        ");                bll.Append("{\r\n            ");                bll.Append("return  dal." + paramsData.UpdateMethodName + "(data);\r\n        ");                bll.Append("}\r\n\r\n");            }            //查询            if (paramsData.CreateSelectMethod)            {                bll.Append("        public static " + deleteThePrefixTheTableName + paramsData.ModelSuffix + " " + paramsData.SelectMethodName + "(" + ChangeToCSharpType(primaryKeyTypeName, false) + " " + parameterName + ")\r\n        ");                bll.Append("{\r\n            ");                bll.Append("return dal." + paramsData.SelectMethodName + "(" + parameterName + ");\r\n        ");                bll.Append("}\r\n\r\n");            }            //查询所有            if (paramsData.CreateSelectAllMethod)            {                bll.Append("        public static IList<" + deleteThePrefixTheTableName + paramsData.ModelSuffix + "> " + paramsData.SelectAllMethodName + "()\r\n        ");                bll.Append("{\r\n            ");                bll.Append("return dal." + paramsData.SelectAllMethodName + "();\r\n        ");                bll.Append("}\r\n\r\n");            }            bll.Append("    }\r\n");            bll.Append("}");            return bll.ToString();        }}

执行文件下载地址:http://pan.baidu.com/s/1sjptNsh  解压密码:shili

源码有进行大量注释,考虑了可扩展性,如果新增其他数据库支持,很容易方便加入。

需要源码的留下邮箱。