Back up more than one database. c#

Asked

Viewed 189 times

0

I’m making an application where I should 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 backup them, I tried to do by Checkedlist Box of c# but I was not successful. The code is simple where it only connects to the instance and backs up 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);
        }
    }
  • You have not succeeded, understood. But why? I mean, what’s the problem? What’s wrong? What do you expect the code to do and what it’s currently doing?

  • Forgive me if it is not clear, I am new in the forum and the doubts sometimes can not be very clear. Anyway, come on. I even made an insertion of a Checkedlist Box, and I can pull the data from the Databases using the command clbDataBase.Items.Add(dr[0].Tostring();. The problem is even selecting more than one bank in the checked List I can not backup it. I’d like some syntax or whatever way I can do it.

  • But why can’t you? What’s stopping you?

  • Because when I select more than one database within Checkedlist it just backs up the first database that was selected, and I want to back up 1.2 or n databases that I select in checkedList.

  • Understood. The checkedList is this cmbDataBase, right?

  • LINQ, excuse me, I screwed up, I had added the wrong code in the text field. Anyway, I put the correct code now. clbDataBase is my checkedList, I can store the banks in it, but I just can’t back up more than one when I make more than one selection

  • WPF ? will have to go through the list items checking which ones are checked

  • Rovann, using a for and checking which box of each row is filled?

  • Windows Form. The only question I have is, how will the condition be? Again I apologize to you, I’m having to learn the most advanced, so I’m still getting the hang of it. for (clbDataBase.Text.CompareTo?)

  • Any result ?

  • I managed to solve here, thanks to you! Much Thank you.

Show 6 more comments

2 answers

0


You will need to go through all the selected items on CheckedList and do the deed.

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!");
  • LINQ, you send very expensive! Thank you very much for the support!

  • You’re welcome, @Matheusarduino V below the arrows to vote, on the left side of the answer.

0

You use the property CheckedItems.Count to check if there are any items selected,

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 name of the files, so you can sort them by date =]

  • Rovann, sorry for the delay in feedback. When I use if (clbDataBase.CheckedItems.Count > 0) and select more than one database it says there is no database selected. But if I put if (clbDataBase.CheckedItems.Count == 0) the program still continues only backing up a bank

  • I edited the answer after the first post, make sure it’s right

  • Rovann still continues to back up only one bank selected in clbDataBase.

  • string sql = "BACKUP DATABASE " + clbDataBase.Checkeditems[i]. Tostring() equals ?

  • the line stayed sql = "BACKUP DATABASE " + clbDataBase.CheckedItems[i].ToString() + " TO DISK ='" + locationBox.Text + "\\" + clbDataBase.CheckedItems[i].ToString() + "-" + DateTime.Now.ToString("yyyy-MM-dd") + ".bak'";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.