Acces denied when i try to save to a location. Its my database to backup...

Ivo

Member
Joined
Feb 8, 2022
Messages
18
Programming Experience
Beginner
Acces denied when try to bakup my database to any place on my harddrive:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SignalWatch
{
    public partial class frmDatabaseBackup : Form
    {
        SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=Workflow;Integrated Security=True");
        public frmDatabaseBackup()
        {
            InitializeComponent();
        }

        private void DatabaseBackup_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnBackupDatabase_Click(object sender, EventArgs e)
        {
            string database = con.Database.ToString();
            if (tbBackupLocation.Text == string.Empty)
            {
                MessageBox.Show("Please enet Backup Location path");
            }
            else
            {
                string cmd = "BACKUP DATABASE[" + database + "] TO DISK= '"+ tbBackupLocation.Text+"\\"+"database"+"_"+DateTime.Now.ToString("dd-MM-yyyy-mm-ss")+".bak'";
                con.Open();
                SqlCommand command = new SqlCommand(cmd,con);
                command.ExecuteNonQuery();
                MessageBox.Show("Database backup done successfuly");
                con.Close();
                btnBackupDatabase.Enabled = false; 
            }
        }

        private void btnBrowseBackup_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                tbBackupLocation.Text = dlg.SelectedPath;
                btnBackupDatabase.Enabled = true;
            }
        }

        private void btnBrowseRestore_Click(object sender, EventArgs e)
        {

        }
    }
}
 
Have you debugged to make sure the location you think your DB is actually is where it is located? Also - is that location write protected? What is the exact error you are getting?
 
Back
Top Bottom