Пример #1
0
        public void Go()
        {
            var fileNames = SqlCommandHelpers.ExecuteSqlQuery(_cfg.MasterConnString, $@"
                SELECT [physical_name] FROM [sys].[master_files]
                WHERE [database_id] = DB_ID('{_cfg.DatabaseName}')",
                                                              row => (string)row["physical_name"]);

            if (fileNames.Any())
            {
                SqlCommandHelpers.ExecuteSqlCommand(_cfg.MasterConnString, $@"
                    ALTER DATABASE [{_cfg.DatabaseName}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
                    EXEC sp_detach_db '{_cfg.DatabaseName}'");

                fileNames.ForEach(File.Delete);
            }
        }
Пример #2
0
        private void TryCreateDatabase()
        {
            try
            {
                SqlCommandHelpers.ExecuteSqlCommand(_cfg.MasterConnString,
                                                    $@"CREATE DATABASE [{_cfg.DatabaseName}]
                       CONTAINMENT = NONE
                       ON  PRIMARY 
                       ( NAME = N'{_cfg.DatabaseName}', FILENAME = N'{_cfg.DatabasePath}' )");
            }
            catch (SqlException ex)
            {
                if (ex.Message.Contains("already exists"))
                {
                    return;
                }

                throw;
            }
        }
Пример #3
0
 private void RunScriptAgainstDatabase(string script) =>
 SqlCommandHelpers.ExecuteSqlCommand(_cfg.ExampleDatabaseConnString, script);