Error while backing up wpf c#

Asked

Viewed 44 times

0

I am having trouble performing backup on my system.

I’m using WPF with SQLSERVER 2008, on that system I need to backup the data on the client’s local machine, then I’m creating the backup directory on Disk C:\\.

However he is returning me this error, I’ve been researching on the subject says it is permission, but I’ve given both permission in sqlserver and in the folder IOS.

follows the error image and my code. inserir a descrição da imagem aqui

    private void backupSistemaToolStripMenuItem_Click(object sender, EventArgs e)
    {
        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 [" + Application.StartupPath + "\\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, 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, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
  • 1

    This is because file creation is delegated to the database, regardless of where the executable is. That is, SQL Server will try to save the backup in this folder on the database server.

  • Good morning Erlon. Dear you could help me solve this problem

1 answer

0

Guys, I got my problem solved. I was providing a parameter instead of the database.

The backup command has this form

backup database NOME_DA_SUA_BASE to DISK=backup path+name_arq.backup with [options]

then correct that line by that

cb string = "backup database SISTEMA_CCA to disk='" + destdir + "'with init,Stats=10";

Problem solved !

Browser other questions tagged

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