我有一个应用程序执行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; } }