private static string BackupInFile(string fileName, string database, SqlConnection connection) { // To perform a backup we first need to get the appropriate backup folder, which is a bit tricky. // First, we need to get the service name. const string query = "SELECT @@SERVICENAME AS name"; SqlCommand cmd = new SqlCommand(query, connection); string serviceName = string.Empty; using (SqlDataReader reader = cmd.ExecuteReader()) { if (null == reader || !reader.HasRows) { return(null); } reader.Read(); if (reader.GetValue(reader.GetOrdinal("name")) != DBNull.Value) { serviceName = reader.GetString(reader.GetOrdinal("name")); } else { serviceName = null; } } // Then get the instance name from the registry const string sqlServerKey = @"SOFTWARE\Microsoft\Microsoft SQL Server"; string key = string.Format(@"{0}\Instance Names\SQL", sqlServerKey); string instanceName = ExtendedRegistry.GetKeyValue(key, serviceName); if (string.IsNullOrEmpty(instanceName)) { return(null); } // Finally, get the backup directory key = string.Format(@"{0}\{1}\MSSQLServer", sqlServerKey, instanceName); string backupDirectory = ExtendedRegistry.GetKeyValue(key, "BackupDirectory"); if (string.IsNullOrEmpty(backupDirectory)) { return(null); } string path = Path.Combine(backupDirectory, fileName); _BackupDatabaseInTempFile(path, database, connection); return(path); }
private static string BackupToFile(string fileName, string database, SqlConnection connection) { string backupDirectory; if (TechnicalSettings.UseDemoDatabase) { backupDirectory = @"C:\Users\Public"; } else { // To perform a backup we first need to get the appropriate backup folder, which is a bit tricky. // First, we need to get the service name. const string query = "SELECT @@SERVICENAME AS name"; OpenCbsCommand cmd = new OpenCbsCommand(query, connection); string serviceName = string.Empty; using (OpenCbsReader reader = cmd.ExecuteReader()) { if (reader.Empty) { return(null); } reader.Read(); serviceName = reader.GetString("name"); } // Then get the instance name from the registry const string sqlServerKey = @"SOFTWARE\Microsoft\Microsoft SQL Server"; string key = string.Format(@"{0}\Instance Names\SQL", sqlServerKey); string instanceName = ExtendedRegistry.GetKeyValue(key, serviceName); if (string.IsNullOrEmpty(instanceName)) { return(null); } // Finally, get the backup directory key = string.Format(@"{0}\{1}\MSSQLServer", sqlServerKey, instanceName); backupDirectory = ExtendedRegistry.GetKeyValue(key, "BackupDirectory"); if (string.IsNullOrEmpty(backupDirectory)) { return(null); } } string path = Path.Combine(backupDirectory, fileName); BackupToFileImpl(path, database, connection); return(path); }