Skip to content
Snippets Groups Projects
Select Git revision
  • 83d12eba2654f34fad5aeb8d6c42f2d4f89de9f5
  • master default protected
  • gitkeep
  • dev protected
  • Issue/2309-docs
  • Issue/1910-MigrationtoNET6.0
  • Sprint/2022-01
  • Sprint/2021-03
  • Product/1287-dotnet5Sharepoint
  • Topic/1334-dotnet5migration
  • Sprint/2021-01
  • Product/407-net5migration
  • Topic/1226-databaseHelpersLibraryMigration
  • v2.1.1
  • v2.1.0
  • v2.0.0
  • v1.2.0
  • v1.1.0
18 results

DatabaseMasterHelper.cs

Blame
  • DatabaseMasterHelper.cs 3.47 KiB
    using Coscine.Configuration;
    using System.Data.SqlClient;
    
    namespace Coscine.Database.Helpers
    {
        public class DatabaseMasterHelper
        {
            private readonly IConfiguration _configuration;
    
            public ConnectionSettings ConnectionSettings { get; set; }
    
            public DatabaseMasterHelper()
            {
                _configuration = new ConsulConfiguration();
            }
    
            public DatabaseMasterHelper(IConfiguration configuration)
            {
                _configuration = configuration;
            }
    
            public void KillConnectionsToDatabase(string dbDatabase)
            {
                var sqlCommandBuilder = new SqlCommandBuilder();
                using (var connection = new SqlConnection(ConnectionSettings.GetConnectionString(false)))
                {
                    connection.Open();
                    var command = connection.CreateCommand();
    
                    command.CommandText = $"alter database {sqlCommandBuilder.QuoteIdentifier(dbDatabase)} set single_user with rollback immediate; alter database {sqlCommandBuilder.QuoteIdentifier(dbDatabase)} set MULTI_USER; ";
                    command.ExecuteNonQuery();
                }
            }
    
            public void CreateDatabase(string dbDatabase)
            {
                var sqlCommandBuilder = new SqlCommandBuilder();
                using (var connection = new SqlConnection(ConnectionSettings.GetConnectionString(false)))
                {
                    connection.Open();
                    var command = connection.CreateCommand();
                    command.CommandText = $"CREATE DATABASE {sqlCommandBuilder.QuoteIdentifier(dbDatabase)};";
                    command.ExecuteNonQuery();
                }
            }
    
            public void DropDatabase(string dbDatabase)
            {
                var sqlCommandBuilder = new SqlCommandBuilder();
                using (var connection = new SqlConnection(ConnectionSettings.GetConnectionString(false)))
                {
                    connection.Open();
                    var command = connection.CreateCommand();
                    command.CommandText = $"DROP DATABASE {sqlCommandBuilder.QuoteIdentifier(dbDatabase)};";
                    command.ExecuteNonQuery();
                }
            }
    
            public bool DatabaseExists(string dbDatabase)
            {
                var sqlCommandBuilder = new SqlCommandBuilder();
                using (var connection = new SqlConnection(ConnectionSettings.GetConnectionString(false)))
                {
                    connection.Open();
                    var command = connection.CreateCommand();
                    var dbName = sqlCommandBuilder.QuoteIdentifier(dbDatabase);
                    dbName = dbName.StartsWith("[") ? dbName.Substring(1) : dbName;
                    dbName = dbName.EndsWith("]") ? dbName.Remove(dbName.Length - 1) : dbName;
                    command.CommandText = $"SELECT DB_ID('{dbName}') AS RESULT;"; ;
                    using (var reader = command.ExecuteReader())