[英]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. 我正在尝试制作一个Windows Forms应用程序,该应用程序能够从SQL Server备份和还原数据库。 The user can choose the location to save the .bak file.
用户可以选择保存.bak文件的位置。
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.
因此,如果我从SQL Server中进行备份并将其保存到C:\\ backup.bak并将其作为“路径”传递到程序中,则它将起作用,但是如果我将文件的位置或名称更改为不存在,它将引发错误,表明它无法打开备份设备。 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?
如果它不存在,是否有可能创建一个新的.bak文件,我将如何去做?
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. 我不熟悉您在此处使用的
Backup
对象(我总是发出实际的BACKUP DATABASE
命令),但我怀疑如果文件不存在vs.如果文件夹不存在vs.没有写该文件夹的权限。
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. 文件是否存在以及是否指定了
.Initialize
( WITH INIT
等效),将决定该情况下命令的行为。
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. 如果该文件夹确实存在,则需要确保SQL Server服务帐户具有写入该文件夹的权限。 You were attempting to write to the root (
C:\\
) - drive roots are special, protected holy ground in modern versions of Windows. 您试图写入根目录(
C:\\
)-驱动器根目录是现代Windows版本中受保护的特殊圣地。 Try creating a folder like C:\\backups\\
and backing up to that location. 尝试创建一个类似于
C:\\backups\\
的文件夹,然后备份到该位置。
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. 这就是为什么我们通常允许SQL Server写入实例的指定备份文件夹或其他一些预定位置的原因-如果需要在其他位置从那里移动/复制文件。 We usually don't want to have to manage permissions for any folder the user might enter.
通常,我们不需要管理用户可能输入的任何文件夹的权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.