简体   繁体   English

C#:将SQL Server数据库备份到新的.bak文件

[英]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. 文件是否存在以及是否指定了.InitializeWITH 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.

相关问题 使用c#创建SQL Server备份文件(.bak)到任何位置 - Creating SQL Server backup file (.bak) with c# to any location 如何在C#中还原SQL Server 2012数据库.bak文件? - How to restore a SQL Server 2012 database .bak file in C#? 使用c#备份数据库,bak文件变得越来越大,如何阻止它增长? - Backup a database using c#, the bak file gets bigger and bigger, how do I stop it from growing? SQL Server使用C#备份到文件 - SQL Server backup to file with C# 使用C#将SQL Server 2008数据库备份到sql文件(如.sql) - Backup SQL Server 2008 database to a sql file(like .sql) using c# 如何使用C#使用新的数据库名称还原Sqlserver .bak文件 - How to Restore Sqlserver .bak file with New database name using c# 如何通过C#以编程方式在不使用.mdf或.ldf文件的情况下将.bak文件还原到新服务器? - How can I restore a .bak file to a new server without using the .mdf or .ldf files, programmatically with C#? 使用C#将SQL Server数据库备份到文本文件(如.csv) - Backup SQL Server database to a text file(like .csv) using c# 使用远程SQL Server上的C#从备份文件创建数据库 - Create database from backup file using C# on the remote SQL Server 使用C#备份SQL Server CE数据库-导入导出 - Backup SQL Server CE database with C# - Import-Export
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM