public static Dictionary <SQLServerSystemFile.Types, SQLServerSystemFile> GetSQLServerSystemFilesByTypes(string sourceConnectionString, ILogger logger) { Dictionary <SQLServerSystemFile.Types, SQLServerSystemFile> sqlServerSystemFilesByTpes = new Dictionary <SQLServerSystemFile.Types, SQLServerSystemFile>(); try { using (SqlConnection con = new SqlConnection(sourceConnectionString)) { con.Open(); using (SqlCommand cmd = new SqlCommand($"SELECT * FROM sys.database_files", con)) { using (SqlDataReader sqlDataReader = cmd.ExecuteReader()) { bool readFromDataRow = false; while (sqlDataReader.Read()) { SQLServerSystemFile.Types type; if (!readFromDataRow) { type = SQLServerSystemFile.Types.Data; readFromDataRow = true; } else { type = SQLServerSystemFile.Types.Log; } sqlServerSystemFilesByTpes[type] = SQLServerSystemFile.CreateFromSqlDataReader(sqlDataReader, type, logger); } } } } } catch (Exception e) { logger.Error(e.Message); return(null); } return(sqlServerSystemFilesByTpes); }
public static bool CreateDatabase(DatabaseFacade newDatabase, ILogger logger) { try { using (SqlConnection connection = new SqlConnection($"Server={newDatabase.DataSource};Integrated security=SSPI;database=master")) { SQLServerSystemFile dataSQLServerSystemFile = newDatabase.SQLServerSystemFilesByTypes[SQLServerSystemFile.Types.Data]; SQLServerSystemFile logSQLServerSystemFile = newDatabase.SQLServerSystemFilesByTypes[SQLServerSystemFile.Types.Log]; connection.Open(); string cmdText = $"CREATE DATABASE {newDatabase.Name} ON PRIMARY " + $"(NAME = {newDatabase.Name}, " + $"FILENAME = '{Path.Combine(dataSQLServerSystemFile.Path, dataSQLServerSystemFile.File)}', " + $"SIZE = {dataSQLServerSystemFile.Size}, " + $"MAXSIZE = {dataSQLServerSystemFile.MaxSize}, " + $"FILEGROWTH = {dataSQLServerSystemFile.Growth}) " + $"LOG ON (NAME = {Path.GetFileNameWithoutExtension(logSQLServerSystemFile.File)}, " + $"FILENAME = '{Path.Combine(logSQLServerSystemFile.Path, logSQLServerSystemFile.File)}', " + $"SIZE = {logSQLServerSystemFile.Size}, " + $"MAXSIZE = {logSQLServerSystemFile.MaxSize}, " + $"FILEGROWTH = {logSQLServerSystemFile.Growth})"; using (SqlCommand command = new SqlCommand(cmdText, connection)) { command.ExecuteNonQuery(); } } } catch (Exception e) { logger.Error(e.Message); return(false); } logger.Information("Created New Database"); return(true); }