I'm making an application where I need to do automatic backups and restore between databases. But I want to solve this step by step.
For now I would like to know how I would make a selection of several databases (Sql Server) and back them up, I tried to do the CheckedList Box from C # but I did not succeed. The code is simple where it only makes the connection to the instance and the backup of 1 database at a time
private void backupButton_Click(object sender, EventArgs e)
{
try
{
if (clbDataBase.Text.CompareTo("") == 0)
{
MessageBox.Show("Por favor selecione um Banco de Dados.");
return;
}
cn = new SqlConnection(connectionString);
cn.Open();
//sql = "BACKUP DATABASE " + cmbDataBase.Text + " TO DISK ='" + locationBox.Text + "\" + cmbDataBase.Text + "-" + DateTime.Now.ToString("dd-MM-yyyy")+ ".bak'";
sql = "BACKUP DATABASE " + clbDataBase.Text + " TO DISK ='" + locationBox.Text + "\" + clbDataBase.Text + "-" + DateTime.Now.ToString("dd-MM-yyyy")+ ".bak'";
cmd = new SqlCommand(sql, cn);
cmd.ExecuteNonQuery();
MessageBox.Show("Backup executado com sucesso!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}