/// <summary>
/// Génère le script de backup
/// </summary>
private string GenerateBackupString()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("BACKUP DATABASE [{0}] ", this.databaseName);
sb.AppendFormat("TO DISK =N'{0}_{1}_{2}.Bak' ", this.textBoxFilePath.Text.Trim(),
this.databaseName, DateTime.Now.Ticks.ToString());
// Ajout des options
sb.Append("WITH ");
// Mode
if (comboBoxMode.SelectedText == "DIFFERENTIAL")
sb.AppendFormat("{0} ", comboBoxMode.SelectedText);
// Options classiques
sb.Append("NOFORMAT, NOINIT, SKIP, NOREWIND, NOUNLOAD, STATS = 10, ");
// Nom
sb.AppendFormat("NAME = N'{0}_{1}', ", this.textBoxBackupName.Text.Trim(),
DateTime.Now.ToString());
// Description
sb.AppendFormat("DESCRIPTION = N'{0}' ", this.textBoxDescription.Text.Trim());
return sb.ToString();
}
/// Make backup
private BackupResult MakeBackup(String script)
SqlConnection connection = new SqlConnection(
BackupByAdoNet.Properties.Settings.Default.PerfSampleConnectionString);
try
// Récupération des informations de sauvegarde
connection.FireInfoMessageEventOnUserErrors = true;
connection.InfoMessage += new SqlInfoMessageEventHandler(connection_InfoMessage);
// Création de la commande
SqlCommand backupCommand = new SqlCommand(script, connection);
connection.Open();
backupCommand.ExecuteNonQuery();
connection.Close();
return new BackupResult("Sauvegarde terminée", DialogResult.OK);
catch (SqlException ex)
if (connection.State != ConnectionState.Closed)
return new BackupResult(String.Format("Sauvegarde échouée: {0}", ex.Message),
DialogResult.Abort);
private void connection_InfoMessage(object sender, SqlInfoMessageEventArgs e)
// Report progress
String message = e.Message;
if (e.Errors.Count > 0 && e.Errors[0].Number == 3211)
String val = message.Substring(0, message.Length - 18);
int percentage = 0;
if (int.TryParse(val, out percentage))
backgroundWorkerBackup.ReportProgress(percentage);