Error Backing Up Windows Forms Database

Asked

Viewed 51 times

0

Guys are having trouble solving this problem! My application is in Windows Forms. I need to perform Backup of data through the application itself, but when I run to perform it informs me of this error.inserir a descrição da imagem aqui

Note: My backup is on a server on the non-local network

This is my code to perform the backup.

 try
        {
            Cursor = Cursors.WaitCursor;
            timer2.Enabled = true;
            if ((!System.IO.Directory.Exists(@"C:\\DBBackup")))
            {
                System.IO.Directory.CreateDirectory(@"C:\\DBBackup");
            }
            string destdir = "C:\\DBBackup\\SISTEMA_CCA " + DateTime.Now.ToString("dd-MM-yyyy_HH-mm-ss") + ".bak";
            cg.con = new SqlConnection(cn.DBconn);
            cg.con.Open();
            string cb = "backup database SISTEMA_CCA to disk='" + destdir + "'with init,stats=10";
            cg.cmd = new SqlCommand(cb);
            cg.cmd.Connection = cg.con;
            cg.cmd.ExecuteReader();
            cg.con.Close();
            st1 = LB_Usuario.Text;
            st2 = "Backup realizado com sucesso";
            cf.LogFunc(st1, System.DateTime.Now, st2);
            MessageBox.Show("Operação concluída com sucesso", "Backup - Banco de dados", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Erro ao Fazer o Backup", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

From now on I thank!

2 answers

4

The message is clear, can’t find the backup file.

Do not forget that when executing the command backup database on the server of SQL-Server, he will look for "C: Dbbackup" on the local disk of the server. If the file is on C: the machine that is running the application will not find the file.

  • Thank you Ricardo Pontual. was a mistake of mine in not observing this detail. thanks worked!

  • quiet, we are here to help :)

0

Take into account some points:

  1. The address indicated in the backup command is always considering as a departure the view from the server where the instance is installed
  2. The user configured in SQL Server Services.Msc must be a user who has proven access to the specified address. It is often advised to use SYSTEM (when backup is done locally) or a domain user if the backup is performed in a remote location (remember to configure the folder accesses)

From what I see in your code, you are creating a folder yes, but you are creating on the machine where the software is running, not on the SQL Server server. Of course, that’s considering the BD server and the software are running on separate machines. If you are running on the same machine, the code is supposed to work (but review points 1 and 2)

Browser other questions tagged

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