Example #1
0
        public static bool Exists(string connectionString, DatabaseType dbType)
        {
            Afx.Data.Entity.Schema.DatabaseSchema databaseSchema = null;
            switch (dbType)
            {
            case DatabaseType.MsSQLServer:
                databaseSchema = new Afx.Data.MSSQLServer.Entity.Schema.MsSqlDatabaseSchema(connectionString);
                break;

            case DatabaseType.MySQL:
                databaseSchema = new Afx.Data.MySql.Entity.Schema.MySqlDatabaseSchema(connectionString);
                break;

            case DatabaseType.SQLite:
                databaseSchema = new Afx.Data.SQLite.Entity.Schema.SQLiteDatabaseSchema(connectionString);
                break;

            case DatabaseType.Oracle:
                databaseSchema = new Afx.Data.Oracle.Entity.Schema.OracleDatabaseSchema(connectionString);
                break;

            default:
                throw new Exception("DatabaseType is error!");
            }
            using (databaseSchema)
            {
                return(databaseSchema.Exist());
            }
        }
Example #2
0
        public static void InitDb <T>(T db) where T : Afx.Data.Entity.EntityContext
        {
            if (!ConfigUtils.InitDatabase || IsInit)
            {
                return;
            }
            lock (lockObj)
            {
                if (IsInit)
                {
                    return;
                }
                string          connectionString = ConfigUtils.ConnectionString;
                IDatabaseSchema databaseSchema   = null;
                ITableSchema    tableSchema      = null;
                switch (ConfigUtils.DatabaseType)
                {
                case DatabaseType.MSSQLServer:
                    databaseSchema = new Afx.Data.MSSQLServer.Entity.Schema.MsSqlDatabaseSchema(connectionString);
                    tableSchema    = new Afx.Data.MSSQLServer.Entity.Schema.MsSqlTableSchema(connectionString);
                    break;

                case DatabaseType.MySQL:
                    databaseSchema = new Afx.Data.MySql.Entity.Schema.MySqlDatabaseSchema(connectionString);
                    tableSchema    = new Afx.Data.MySql.Entity.Schema.MySqlTableSchema(connectionString);
                    break;

                default:
                    throw new Exception("【InitDatabase】InitDb, 数据库类型错误!");
                }
                //更新数据库结构sql 日志
                databaseSchema.Log = WriteSQL;
                tableSchema.Log    = WriteSQL;
                using (var build = new BuildDatabase(databaseSchema, tableSchema))
                {
                    build.Build <T>();
                }

                IsInit = true;
            }
        }
Example #3
0
        internal static void Initialize(FileContext context, Type dbContextType)
        {
            if (!ConfigUtils.InitDatabase)
            {
                return;
            }

            lock (lockObj)
            {
                if (!AfxTcpFileServerSample.Common.ConfigUtils.InitDatabase)
                {
                    return;
                }
                context.WriteSQL("-- Initialize Database begin");
                try
                {
                    Afx.Data.Entity.Schema.DatabaseSchema databaseSchema = null;
                    Afx.Data.Entity.Schema.TableSchema    tableSchema    = null;
                    switch (ConfigUtils.DatabaseType)
                    {
                    case DatabaseType.MsSQLServer:
                        databaseSchema = new Afx.Data.MSSQLServer.Entity.Schema.MsSqlDatabaseSchema(ConfigUtils.ConnectionString);
                        tableSchema    = new Afx.Data.MSSQLServer.Entity.Schema.MsSqlTableSchema(ConfigUtils.ConnectionString);
                        break;

                    case DatabaseType.MySQL:
                        databaseSchema = new Afx.Data.MySql.Entity.Schema.MySqlDatabaseSchema(ConfigUtils.ConnectionString);
                        tableSchema    = new Afx.Data.MySql.Entity.Schema.MySqlTableSchema(ConfigUtils.ConnectionString);
                        break;

                    case DatabaseType.SQLite:
                        databaseSchema = new Afx.Data.SQLite.Entity.Schema.SQLiteDatabaseSchema(ConfigUtils.ConnectionString);
                        tableSchema    = new Afx.Data.SQLite.Entity.Schema.SQLiteTableSchema(ConfigUtils.ConnectionString);
                        break;

                    case DatabaseType.Oracle:
                        databaseSchema = new Afx.Data.Oracle.Entity.Schema.OracleDatabaseSchema(ConfigUtils.ConnectionString);
                        tableSchema    = new Afx.Data.Oracle.Entity.Schema.OracleTableSchema(ConfigUtils.ConnectionString);
                        break;

                    default:
                        throw new Exception("DatabaseType is error!");
                    }

                    if (databaseSchema != null && tableSchema != null)
                    {
                        databaseSchema.Log = context.WriteSQL;
                        tableSchema.Log    = context.WriteSQL;
                        using (var builder = new Afx.Data.Entity.Schema.BuildDatabase(databaseSchema, tableSchema))
                        {
                            builder.Build <FileContext>();
                        }
                    }

                    context.WriteSQL("-- InitBaseData begin");
                    new InitDb().InitBaseData(context);
                    context.WriteSQL("-- InitBaseData end");
                    context.WriteSQL("-- Initialize Database end");
                }
                catch (Exception ex)
                {
                    LogUtils.Error("【InitDataBase】", ex);
                }
            }
        }