C# · 12月 26, 2021

C#将远程数据库的SMO备份到本地机器

我有一个应用程序执行sql数据库的备份和恢复,这在本地机器上工作正常,但是如果我针对在另一台机器上托管的sql服务器运行它,我会收到以下错误

Microsoft.sqlServer.Management.Smo.FailedOperationException: Backup Failed for Server ‘25.98.30.79’. —> Microsoft.sqlServer.Management.Common.ExecutionFailureException: An exception occurred while executing a Transact-sql statement or batch. —> System.Data.sqlClient.sqlException: Cannot open backup device ‘C:\Program Files\State Manager\Archive\Capture\20100217152147*product*\databases*database**database*.bak’. Operating system error 3(The system cannot find the path specified.).

这似乎是由sql服务器尝试将此文件写入其本地驱动器引起的.由于安全限制,我无法设置可以放置备份的共享区域.

有人知道如何将这些数据移回机器代码正在被调用?

我的代码如下

private string Name; private string Server; private string dbName; private string user; private string password; public Boolean performCapture(String archiveDir) { String destination = archiveDir + “\\” + Name; if (!System.IO.Directory.Exists(destination)) { System.IO.Directory.CreateDirectory(destination); } Server sqlServer = connect(); if (sqlServer != null) { DatabaseCollection dbc = sqlServer.Databases; if (dbc.Contains(dbName)) { Backup bkpDatabase = new Backup(); bkpDatabase.Action = BackupActionType.Database; bkpDatabase.Database = dbName; BackupDeviceItem bkpDevice = new BackupDeviceItem(destination + “\\” + dbName + “.bak”,DeviceType.File); bkpDatabase.Devices.Add(bkpDevice); bkpDatabase.Incremental = false; bkpDatabase.Initialize = true; // Perform the backup bkpDatabase.sqlBackup(sqlServer); if (System.IO.File.Exists(destination + “\\” + dbName + “.bak”)) { return true; } else { return false; } } else { return false; } } else { return false; } }解决方法 不,这不会有效 – sql Server只能备份到实际连接到实际sql Server计算机的驱动器.您无法在任何情况下将远程sql Server备份到本地硬盘 – 只是不可能(SMO或sql Server Management Studio中).