Created
November 16, 2011 14:49
-
-
Save drusellers/1370223 to your computer and use it in GitHub Desktop.
Dropping a PGDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace helper | |
{ | |
using System.Data; | |
using System.Data.SqlClient; | |
using System.Diagnostics; | |
using FluentNHibernate.Cfg.Db; | |
using Magnum.Extensions; | |
using Npgsql; | |
public class DatabaseUtility | |
{ | |
public static void DropPgDatabase(string DB_NAME) | |
{ | |
ExecutePgSql("SELECT pg_terminate_backend(pg_stat_activity.procpid) FROM pg_stat_activity WHERE pg_stat_activity.datname='{0}'".FormatWith(DB_NAME)); | |
ExecutePgSql("DROP DATABASE {0}".FormatWith(DB_NAME)); | |
} | |
public static void CreatePgDatabase(string DB_NAME) | |
{ | |
ExecutePgSql(@"CREATE DATABASE {0} | |
WITH OWNER = fluent | |
ENCODING = 'UTF8' | |
TABLESPACE = pg_default | |
LC_COLLATE = 'English_United States.1252' | |
LC_CTYPE = 'English_United States.1252' | |
CONNECTION LIMIT = -1;".FormatWith(DB_NAME)); | |
ExecutePgSql("VACUUM"); | |
} | |
static void ExecutePgSql(string sql) | |
{ | |
PostgreSQLConfiguration cfg = PostgreSQLConfiguration.PostgreSQL82.ConnectionString(x => | |
{ | |
x.Username("my_user_name"); | |
x.Port(5432); | |
x.Password("my_secret_password"); | |
x.Host("localhost"); | |
x.Database("my_db_name"); | |
}); | |
using (var conn = new NpgsqlConnection(cfg.ToProperties()["connection.connection_string"])) | |
using (NpgsqlCommand cmd = conn.CreateCommand()) | |
{ | |
conn.Open(); | |
cmd.CommandText = sql; | |
cmd.CommandType = CommandType.Text; | |
cmd.ExecuteNonQuery(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment