简体   繁体   中英

C#: Backup SQL Server Database to a NEW .bak file

I'm trying to make a Windows Forms app that will be able to backup and restore a database from SQL Server. The user can choose the location to save the .bak file.

Problem I'm having is that if the file does not already exist it doesn't create a new one. So if I do a backup from within SQL Server and save it to C:\\backup.bak and pass that into the program as the "path" it works, if I however change the location or the name of the file to one that does not exist it throws an error saying it cannot open backup device. Is it at all possible to have it create a new .bak file if it doesn't exist and how would I go about doing it?

My code currently is as follows;

            var dataSource = txtDS.Text;
            var db = txtDbName.Text;
            var path = txtBackup.Text;
            var name = txtName.Text;
            var desc = txtDesc.Text;

            if(File.Exists( path) == true )
            {
                MessageBox.Show("Test");
            }
            else
            {

                Directory.CreateDirectory(@path);
            }

            Server myServer = new Server(dataSource);
            myServer.ConnectionContext.LoginSecure = true;
            myServer.ConnectionContext.Connect();

            var bkpDbLog = new Backup();
            bkpDbLog.Action = BackupActionType.Database;
            bkpDbLog.Database = db;

            bkpDbLog.Devices.AddDevice(path, DeviceType.File);
            bkpDbLog.BackupSetName = name;
            bkpDbLog.BackupSetDescription = desc;

            bkpDbLog.Initialize = true;
            bkpDbLog.Checksum = true;
            bkpDbLog.ContinueAfterError = true;
            bkpDbLog.Incremental = false;
            bkpDbLog.FormatMedia = false;

            bkpDbLog.PercentComplete += CompletionStatusInPercent;

            bkpDbLog.Complete += BackupCompleted;

            bkpDbLog.SqlBackup(myServer);

            if (myServer.ConnectionContext.IsOpen)
                myServer.ConnectionContext.Disconnect();

Thanks!

I'm not familiar with the Backup object you're using here (I always issue actual BACKUP DATABASE commands) but I suspect you'll get different errors if the file does not exist vs. if the folder does not exist vs. if you don't have permissions to write to the folder.

Whether the file exists or not, and whether you've specified .Initialize (the equivalent of WITH INIT ), will dictate how the command behaves in that case.

If the folder doesn't exist, you will get an error, and you will need to create the folder (well, the entire path, really) first.

If the folder does exist, you need to ensure the SQL Server service account has permissions to write to that folder. You were attempting to write to the root ( C:\\ ) - drive roots are special, protected holy ground in modern versions of Windows. Try creating a folder like C:\\backups\\ and backing up to that location.

This is kind of why we typically allow SQL Server to write to the designated backup folder for the instance, or some other pre-determined location - then move/copy the file from there if we need it elsewhere. We usually don't want to have to manage permissions for any folder the user might enter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM