I looked at some tutorials here on the site, but I did not find one that fits with what I'm looking for, so I followed this tutorial Backup & Restore Sql Server database in C #
It even has a link in the comments with the codes already ready, but when I click to create the DB backup it appears this error:
Idonotknowifitmakesadifference,IdonotuseSqlServerManagement,IuseVisualStudio2010directly,doesanyoneknowhowtosolvetheproblem?
PS1:
privatevoidbtn_criar_Click(objectsender,EventArgse){stringdatabase=con.Database.ToString();try{if(path_criar.Text==string.Empty){MessageBox.Show("please enter backup file location");
}
else
{
string cmd = "BACKUP DATABASE [" + database + "] TO DISK='" + path_criar.Text + "\" + "database" + "-" + DateTime.Now.ToString("yyyy-MM-dd--HH-mm-ss") + ".bak'";
using (SqlCommand command = new SqlCommand(cmd, con))
{
if (con.State != ConnectionState.Open)
{
con.Open();
}
command.ExecuteNonQuery();
con.Close();
MessageBox.Show("database backup done successefully");
btn_criar.Enabled = false;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
The part of line 54 is this command.ExecuteNonQuery();
PS2: I discovered that if I clicked on backup again shortly after closing the error, it backs up or restores depending on what I choose, so I "got it", put a btn_criar.PerformClick () ; so I clicked the button again instead of the error appears, but I would like to solve the problem because I do not know what problems can happen in the future (and yes, I already tested and the backup works)