使用SMO方法备份数据库

时间:2017-07-06 03:33:32

标签: c# sql-server

我想用SMO方法从数据库中获取备份,下面是我的代码:

using Microsoft.SqlServer.Server;
using Microsoft.SqlServer.Management;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.Data.SqlClient;
using Microsoft.Win32;
using System.IO;

Backup backupdb = new Backup();
                        backupdb.Action = BackupActionType.Database;
                        backupdb.Database = "PCPdb";
                        Microsoft.Win32.SaveFileDialog savefd = new Microsoft.Win32.SaveFileDialog();
                        savefd.Filter = "Backup File (*.Bak)|*.Bak";
                        savefd.FileName = DateTime.Now.ToString("ddMMyyyy");
                        Nullable<bool> result = savefd.ShowDialog();
                        if (result == true)
                        {
                            BackupDeviceItem bkpDevice = new BackupDeviceItem(savefd.FileName, DeviceType.File);
                            backupdb.Devices.Add(bkpDevice);
                            backupdb.SqlBackup(srvr);
                           //MessageBox.Show("Backup Database Successfully");

但我得到了这样的例外#34; **backupdb.SqlBackup(srvr);**&#34; Microsoft.SqlServer.Management.Smo.FailedOperationException&#39;发生在Microsoft.SqlServer.SmoExtended.dll

0 个答案:

没有答案