我正在尝试通过 c# 代码将本地 SQL Server 数据库备份到我的本地计算机,但我收到 Backup failed for server 。
代码如下:
using (var con = new SqlConnection(conn))
{
const string destination = "d:\\backups"+"/";
var sqlServer = new Server(new ServerConnection(con));
con.Open();
var bkpDatabase = new Backup { Action = BackupActionType.Database, Database = name };
var bkpDevice = new BackupDeviceItem(destination + name + ".bak", DeviceType.File);
bkpDatabase.Devices.Add(bkpDevice);
bkpDatabase.Checksum = true;
bkpDatabase.ContinueAfterError = true;
bkpDatabase.Incremental = false;
bkpDatabase.Initialize = true;
// Perform the backup
bkpDatabase.SqlBackup(sqlServer);
con.Close();
}
我使用了以下参考资料:
mircrosoft.sqlserver.smo;
mircrosoft.sqlserver.smoextended;
mircrosoft.sqlserver.management.sdk.sfc;
mircrosoft.sqlserver.connectioninfo;
内部异常:
Microsoft.SqlServer.Management.Smo.FailedOperationException was caught
HelpLink=http://go.microsoft.com/fwlink? ProdName=Microsoft+SQL+Server&ProdVer=10.50.2500.0+((KJ_PCU_Main).110617- 0026+)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptio nText&EvtID=Backup+Server&LinkId=20476
HResult=-2146233088
Message=Backup failed for Server 'XXXXX'.
Source=Microsoft.SqlServer.SmoExtended
Operation=Backup
StackTrace:
at Microsoft.SqlServer.Management.Smo.Backup.SqlBackup(Server srv)
at DatabaseCreation.Program.BackupDatabase(String name, String conn) in c:\Users\Documents\Visual Studio 2013\Projects\Databaseinstallation\DatabaseCreation\Program.cs:line 154
InnerException: System.NullReferenceException
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=Microsoft.SqlServer.ConnectionInfo
StackTrace:
at Microsoft.SqlServer.Management.Common.ConnectionManager.get_IsOpen()
at Microsoft.SqlServer.Management.Common.ConnectionManager.Connect()
at Microsoft.SqlServer.Management.Common.ConnectionManager.PoolConnect()
at Microsoft.SqlServer.Management.Common.ConnectionManager.get_DatabaseEngineType()
at Microsoft.SqlServer.Management.Smo.Server.GetExecutionManager()
at Microsoft.SqlServer.Management.Smo.Server.get_ExecutionManager()
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.get_ServerVersion()
at Microsoft.SqlServer.Management.Smo.Backup.Script(Server targetServer)
at Microsoft.SqlServer.Management.Smo.Backup.SqlBackup(Server srv)