Error Backing Up Windows Forms Database

0

I have problems with this problem! My application is in Windows Forms. I need to back up the data through the application itself, but when I run to perform it it informs me of this error. https://i.stack.imgur.com/e5cq8.jpg ">

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

This is my code to back up.

 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);
        }

Thank you in advance!

    
asked by anonymous 21.06.2018 / 18:04

2 answers

4

The message is clear, can not find the backup file.

Do not forget that when you run the backup database command on the SQL-Server server, it will look for "C: \ DBBackup" on the server's local disk. If the file is in C: of the machine that is running the application will not find the file.

    
21.06.2018 / 18:11
0

Consider some points:

  • The address given in the backup command is always considered starting the view from the server where the instance is installed
  • The user configured in SQL Server Services.msc must be a user who has proven access to the given address. It is often advised to use SYSTEM (when backed up locally) or a domain user if backup is performed remotely (remember to configure the accesses of the folder)
  • From what I see in your code, you are creating a yes folder, but you are creating it on the machine where the software is running, not on the SQL Server server. Of course, considering the BD server and software are running on separate machines. If they are running on the same machine, the code is supposed to work (but review points 1 and 2)

        
    28.06.2018 / 11:18