Ejemplo 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  * from {0} limit 0,1", 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);
                    }
                }
            }
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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  * from {0} limit 0,1", tableName));
                    string className    = db.GetClassTypeByTableName(tableName);
                    callBack(tables, className, tableName);
                }
            }
        }