0
I looked in some tutorials here on the site, but I did not find one that fits 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 backup of DB appears this error:
I don’t know if it makes a difference, I don’t use Sql Server Management, direct use in Visual Studio 2010, anyone can tell me how to solve the problem?
PS1:
private void btn_criar_Click(object sender, EventArgs e)
{
string database = 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 found out that if I click on the backup again soon after closing the error, it either backs up or restores depending on what I choose, then I "engemble" it, put in "catch" a btn_create.Performclick(); for it to click the button again instead of the error appear, but I would like to solve the problem because I don’t know what problems can happen in the future (and yes, I have tried it and the backup works)
Henry is welcome to the community, I recommend you to do the tour to understand how the community works, first try to do something, then ask the question by exposing your question and or problem.
– user81560
The first error reported is a misreported alias, where it seems to generate a sequence of errors. Henrique posts the code for his backup.Cs, specifically the method area for line 54.
– Paulo Ricardo