Esempio n. 1
0
        /// <summary>
        /// 创建SQL实体文件
        /// </summary>
        /// <param name="db"></param>
        /// <param name="fileDirectory"></param>
        /// <param name="nameSpace">命名空间(默认:null)</param>
        /// <param name="tableOrView">是生成视图文件还是表文件,null生成表和视图,true生成表,false生成视图(默认为:null)</param>
        public void CreateClassFiles(SqlSugarClient db, string fileDirectory, string nameSpace = null, bool?tableOrView = null, Action <string> callBack = null)
        {
            string sql    = GetCreateClassSql(tableOrView);
            var    tables = db.GetDataTable(sql);

            if (tables != null && tables.Rows.Count > 0)
            {
                foreach (DataRow dr in tables.Rows)
                {
                    string tableName    = dr["name"].ToString();
                    var    currentTable = db.GetDataTable(string.Format("select top 1 * from {0}", tableName));
                    if (callBack != null)
                    {
                        var    tableColumns = GetTableColumns(db, tableName);
                        var    classCode    = DataTableToClass(currentTable, tableName, nameSpace, tableColumns);
                        string className    = db.GetClassTypeByTableName(tableName);
                        classCode = classCode.Replace("class " + tableName, "class " + className);
                        FileSugar.CreateFile(fileDirectory.TrimEnd('\\') + "\\" + className + ".cs", classCode, Encoding.UTF8);
                        callBack(className);
                    }
                    else
                    {
                        var    tableColumns = GetTableColumns(db, tableName);
                        string className    = db.GetClassTypeByTableName(tableName);
                        var    classCode    = DataTableToClass(currentTable, className, nameSpace, tableColumns);
                        FileSugar.CreateFile(fileDirectory.TrimEnd('\\') + "\\" + className + ".cs", classCode, Encoding.UTF8);
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        ///  创建SQL实体文件,指定表名
        /// </summary>
        public void CreateClassFilesByTableNames(SqlSugarClient db, string fileDirectory, string nameSpace, params string[] tableNames)
        {
            var isLog = db.IsEnableLogEvent;

            db.IsEnableLogEvent = false;
            string sql    = SqlSugarTool.GetCreateClassSql(null);
            var    tables = db.GetDataTable(sql);

            if (!FileSugar.IsExistDirectory(fileDirectory))
            {
                FileSugar.CreateDirectory(fileDirectory);
            }
            if (tables != null && tables.Rows.Count > 0)
            {
                foreach (DataRow dr in tables.Rows)
                {
                    string tableName = dr["name"].ToString().ToLower();
                    if (tableNames.Any(it => it.ToLower() == tableName))
                    {
                        var    currentTable = db.GetDataTable(string.Format(SqlSugarTool.GetSelectTopSql(), GetTableNameWithSchema(db, tableName).GetTranslationSqlName()));
                        var    tableColumns = GetTableColumns(db, tableName);
                        string className    = db.GetClassTypeByTableName(tableName);
                        var    classCode    = DataTableToClass(currentTable, className, nameSpace, tableColumns);
                        FileSugar.CreateFile(fileDirectory.TrimEnd('\\') + "\\" + className + ".cs", classCode, Encoding.UTF8);
                    }
                }
            }
            db.IsEnableLogEvent = isLog;
        }
        /// <summary>
        ///  创建SQL实体文件,指定表名
        /// </summary>
        public void CreateClassFilesByTableNames(SqlSugarClient db, string fileDirectory, string nameSpace, params string[] tableNames)
        {
            string sql    = GetCreateClassSql(null);
            var    tables = db.GetDataTable(sql);

            if (!FileSugar.IsExistDirectory(fileDirectory))
            {
                FileSugar.CreateDirectory(fileDirectory);
            }
            if (tables != null && tables.Rows.Count > 0)
            {
                foreach (DataRow dr in tables.Rows)
                {
                    string tableName = dr["name"].ToString().ToLower();
                    if (tableNames.Any(it => it.ToLower() == tableName))
                    {
                        var    currentTable = db.GetDataTable(string.Format("select top 1 * from {0}", tableName));
                        var    tableColumns = GetTableColumns(db, tableName);
                        string className    = db.GetClassTypeByTableName(tableName);
                        var    classCode    = DataTableToClass(currentTable, className, nameSpace, tableColumns);
                        FileSugar.CreateFile(fileDirectory.TrimEnd('\\') + "\\" + className + ".cs", classCode, Encoding.UTF8);
                    }
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 创建实体文件
        /// </summary>
        /// <param name="db"></param>
        /// <param name="fileDirectory"></param>
        /// <param name="nameSpace">命名空间(默认:system)</param>
        /// <param name="tableOrView">是生成视图文件还是表文件,null生成表和视图,true生成表,false生成视图(默认为:null)</param>
        /// <param name="callBack">生成文件后的处理,参数string为实体名</param>
        /// <param name="callBack">生成文件前的处理,参数string为表名</param>
        public void CreateClassFiles(SqlSugarClient db, string fileDirectory, string nameSpace = null, bool?tableOrView = null, Action <string> callBack = null, Action <string> preAction = null)
        {
            var isLog = db.IsEnableLogEvent;

            db.IsEnableLogEvent = false;
            string sql    = SqlSugarTool.GetCreateClassSql(tableOrView);
            var    tables = db.GetDataTable(sql);

            if (tables != null && tables.Rows.Count > 0)
            {
                foreach (DataRow dr in tables.Rows)
                {
                    string tableName = dr["name"].ToString();
                    if (preAction != null)
                    {
                        preAction(tableName);
                    }
                    var currentTable = db.GetDataTable(string.Format(SqlSugarTool.GetSelectTopSql(), GetTableNameWithSchema(db, tableName).GetTranslationSqlName()));
                    if (callBack != null)
                    {
                        var    tableColumns = GetTableColumns(db, tableName);
                        var    classCode    = DataTableToClass(currentTable, tableName, nameSpace, tableColumns);
                        string className    = db.GetClassTypeByTableName(tableName);
                        classCode = classCode.Replace("class " + tableName, "class " + className);
                        FileSugar.CreateFile(fileDirectory.TrimEnd('\\') + "\\" + className + ".cs", classCode, Encoding.UTF8);
                        callBack(className);
                    }
                    else
                    {
                        var    tableColumns = GetTableColumns(db, tableName);
                        string className    = db.GetClassTypeByTableName(tableName);
                        var    classCode    = DataTableToClass(currentTable, className, nameSpace, tableColumns);
                        FileSugar.CreateFile(fileDirectory.TrimEnd('\\') + "\\" + className + ".cs", classCode, Encoding.UTF8);
                    }
                }
            }
            db.IsEnableLogEvent = isLog;
        }