Esempio n. 1
0
        private static void _CreateDatabase(DbProviderFactory provider, string connectionString, string providerName)
        {
            string dbName, dbFile;
            string connStr = _StripDbName(connectionString, providerName, out dbName, out dbFile);
            var    command = new StringBuilder(); // Build SQL Command..

            if (providerName == SQLiteProvider)
            {
                // Do nothing..
                return;
            }

            if (providerName == SqlCe)
            {
                if (File.Exists(dbName))
                {
                    File.Delete(dbName);
                }

                Type         type = Type.GetType("System.Data.SqlServerCe.SqlCeEngine, System.Data.SqlServerCe");
                PropertyInfo localConnectionString = type.GetProperty("LocalConnectionString");
                MethodInfo   createDatabase        = type.GetMethod("CreateDatabase");

                object engine = Activator.CreateInstance(type);
                localConnectionString.SetValue(engine, string.Format("Data Source='{0}';", dbName), null);

                createDatabase.Invoke(engine, new object[0]);

                return;
            }

            if (providerName == Firebird)
            {
                if (File.Exists(dbName))
                {
                    File.Delete(dbName);
                }

                Type       type           = Type.GetType("FirebirdSql.Data.FirebirdClient.FbConnection, FirebirdSql.Data.FirebirdClient");
                MethodInfo createDatabase = type.GetMethod(
                    "CreateDatabase",
                    new[]
                {
                    typeof(string), typeof(int), typeof(bool),
                    typeof(bool)
                });

                object engine = Activator.CreateInstance(type);
                createDatabase.Invoke(engine, new object[] { connectionString, 8192, true, false });

                return;
            }
            else if (providerName == OracleDataProvider)
            {
                throw new NotImplementedException();
            }
            else if (providerName == PostgreSQLProvider)
            {
                command.AppendFormat(CultureInfo.InvariantCulture, "CREATE DATABASE \"{0}\" WITH ENCODING = 'UTF8'", dbName);
            }
            else if (providerName == MsSqlProvider)
            {
                command.AppendFormat(CultureInfo.InvariantCulture, "CREATE DATABASE [{0}] ", dbName);

                // Handle MSSQL AttachDBFile..
                if (providerName == MsSqlProvider && !string.IsNullOrEmpty(dbFile))
                {
                    string fname    = Path.GetFileNameWithoutExtension(dbFile);
                    string pathname = Path.Combine(Path.GetDirectoryName(dbFile), fname);

                    command.AppendFormat(
                        CultureInfo.InvariantCulture,
                        "ON PRIMARY (NAME = {0}, FILENAME = '{1}.mdf', SIZE = 10MB) " + "LOG ON (NAME = {0}_log, FILENAME = '{1}.ldf', SIZE = 2MB)",
                        fname,
                        pathname);
                }
            }

            log.DebugFormat(CultureInfo.InvariantCulture, "Creating Database '{0}..", dbName);
            provider.ExecuteNonQuery(connStr, command.ToString());
            log.InfoFormat(CultureInfo.InvariantCulture, "Database instance '{0}' created!", dbName);
        }