Exemplo 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);
                    }
                }
            }
        }
        /// <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 CreateClassFilesInterface(SqlSugarClient db, bool?tableOrView, Action <DataTable, string, string> callBack)
        {
            var tables = db.GetDataTable("select name from sysobjects where xtype in ('U','V') ");

            if (tableOrView != null)
            {
                if (tableOrView == true)
                {
                    tables = db.GetDataTable("select name from sysobjects where xtype in ('U') ");
                }
                else
                {
                    tables = db.GetDataTable("select name from sysobjects where xtype in ('V') ");
                }
            }
            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));
                    string className    = db.GetClassTypeByTableName(tableName);
                    callBack(tables, className, tableName);
                }
            }
        }
Exemplo n.º 3
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);
                    }
                }
            }
        }
        /// <summary>
        /// 获取所有数据库表名
        /// </summary>
        /// <param name="db"></param>
        /// <returns></returns>
        public List <string> GetTableNames(SqlSugarClient db)
        {
            var tableNameList = db.SqlQuery <string>("select name from sysobjects where xtype in ('U','V') ").ToList();

            for (int i = 0; i < tableNameList.Count; i++)
            {
                var tableName = tableNameList[i];
                tableNameList[i] = db.GetClassTypeByTableName(tableName);
            }
            return(tableNameList);
        }
Exemplo n.º 6
0
        /// <summary>
        /// 获取所有数据库表名
        /// </summary>
        /// <param name="db"></param>
        /// <returns></returns>
        public List <string> GetTableNames(SqlSugarClient db)
        {
            string sql           = GetCreateClassSql(null);
            var    tableNameList = db.SqlQuery <string>(sql).ToList();

            for (int i = 0; i < tableNameList.Count; i++)
            {
                var tableName = tableNameList[i];
                tableNameList[i] = db.GetClassTypeByTableName(tableName);
            }
            return(tableNameList);
        }
Exemplo n.º 7
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;
        }
Exemplo n.º 8
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 CreateClassFilesInterface(SqlSugarClient db, bool?tableOrView, Action <DataTable, string, string> callBack)
        {
            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));
                    string className    = db.GetClassTypeByTableName(tableName);
                    callBack(tables, className, tableName);
                }
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// 获取所有数据库表名
        /// </summary>
        /// <param name="db"></param>
        /// <returns></returns>
        public List <string> GetTableNames(SqlSugarClient db)
        {
            var isLog = db.IsEnableLogEvent;

            db.IsEnableLogEvent = false;
            string sql           = SqlSugarTool.GetCreateClassSql(null);
            var    tableNameList = db.SqlQuery <string>(sql).ToList();

            for (int i = 0; i < tableNameList.Count; i++)
            {
                var tableName = tableNameList[i];
                tableNameList[i] = db.GetClassTypeByTableName(tableName);
            }
            db.IsEnableLogEvent = isLog;
            return(tableNameList);
        }
Exemplo n.º 10
0
        /// <summary>
        /// 创建SQL实体文件
        /// </summary>
        /// <param name="db"></param>
        /// <param name="tableOrView">是生成视图文件还是表文件,null生成表和视图,true生成表,false生成视图(默认为:null)</param>
        /// <param name="callBack">回调函数</param>
        public void CreateClassFilesInterface(SqlSugarClient db, bool?tableOrView, Action <DataTable, string, string> callBack)
        {
            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();
                    var    currentTable = db.GetDataTable(string.Format(SqlSugarTool.GetSelectTopSql(), GetTableNameWithSchema(db, tableName).GetTranslationSqlName()));
                    string className    = db.GetClassTypeByTableName(tableName);
                    callBack(tables, className, tableName);
                }
            }
            db.IsEnableLogEvent = isLog;
        }
Exemplo n.º 11
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)
        {
            var tables = db.GetDataTable("select name from sysobjects where xtype in ('U','V') ");

            if (tableOrView != null)
            {
                if (tableOrView == true)
                {
                    tables = db.GetDataTable("select name from sysobjects where xtype in ('U') ");
                }
                else
                {
                    tables = db.GetDataTable("select name from sysobjects where xtype in ('V') ");
                }
            }
            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.WriteText(fileDirectory.TrimEnd('\\') + "\\" + className + ".cs", classCode);
                        callBack(className);
                    }
                    else
                    {
                        var classCode = DataTableToClass(currentTable, tableName, nameSpace);
                        FileSugar.WriteText(fileDirectory.TrimEnd('\\') + "\\" + tableName + ".cs", classCode);
                    }
                }
            }
        }