2

我正在尝试通过 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)
4

1 回答 1

2

也许您必须转义\目标路径中的角色:

@"d:\backups"

或者

"d:\\backups"

尝试以下操作:

using (var con = new SqlConnection(conn))
{
    const string destination = "c:\\backups";
    var fileName = Path.Combine(destination, String.Format("{0}.bak", name));
    if (!Directory.Exists(destination))
        Directory.CreateDirectory(destination);
    var sqlServer = new Server(new ServerConnection(con));
    var bkpDatabase = new Backup
    {
        Action = BackupActionType.Database,
        Database = name
    };
    var bkpDevice = new BackupDeviceItem(fileName, DeviceType.File);
    bkpDatabase.Devices.Add(bkpDevice);
    bkpDatabase.Checksum = true;
    bkpDatabase.ContinueAfterError = true;
    bkpDatabase.Incremental = false;
    bkpDatabase.Initialize = true;
    bkpDatabase.SqlBackup(sqlServer);
}
于 2013-10-29T14:22:13.070 回答