Make a backup in more than one database. W#

0

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);
        }
    }
    
asked by anonymous 07.06.2017 / 21:18

2 answers

0

You will need to go through all the selected items in CheckedList and do the action.

Something like:

if(clbDataBase.CheckedItems.Count == 0)
{  
    MessageBox.Show("Por favor selecione um Banco de Dados.");
    return;
}

foreach(var marcado in clbDataBase.CheckedItems)
{
    cn = new SqlConnection(connectionString);
    cn.Open();

    sql = "BACKUP DATABASE " + marcado.ToString() + " TO DISK ='" + 
           locationBox.Text + "\" + marcado.ToString() + 
           "-" + DateTime.Now.ToString("dd-MM-yyyy")+ ".bak'";

    cmd = new SqlCommand(sql, cn);
    cmd.ExecuteNonQuery();
}


MessageBox.Show("Backups executados com sucesso!");
    
07.06.2017 / 22:04
0

You use the CheckedItems.Count property to check for any selected items,

Open the connection to your base, and scroll through the selected items by performing the processing you need.

    private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            if (clbDataBase.CheckedItems.Count ==0)
            {
                MessageBox.Show("Por favor selecione um Banco de Dados.");
            }
            else
            {
                SqlConnection cn = new SqlConnection(connectionString);
                cn.Open();

                for (int i = 0; i < clbDataBase.CheckedItems.Count; i++)
                {

                    string sql = "BACKUP DATABASE " + clbDataBase.CheckedItems[i].ToString() + " TO DISK ='" + locationBox.Text + "\" + clbDataBase.CheckedItems[i].ToString() + "-" + DateTime.Now.ToString("yyyy-MM-dd") + ".bak'";
                    SqlCommand cmd = new SqlCommand(sql, cn);
                    cmd.ExecuteNonQuery();
                }
                MessageBox.Show("Backup executado com sucesso!");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

ps. A hint, Use yyyy-MM-dd in the filenames, so you can sort them by date =]

    
07.06.2017 / 22:06