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?
– Jéf Bueno
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.
– Matheus Arduino
But why can’t you? What’s stopping you?
– Jéf Bueno
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.
– Matheus Arduino
Understood. The checkedList is this
cmbDataBase
, right?– Jéf Bueno
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
– Matheus Arduino
WPF ? will have to go through the list items checking which ones are checked
– Rovann Linhalis
Rovann, using a for and checking which box of each row is filled?
– Matheus Arduino
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?)
– Matheus Arduino
Any result ?
– Rovann Linhalis
I managed to solve here, thanks to you! Much Thank you.
– Matheus Arduino