0

使用 C# 和 SMO,当我创建备份时,它们被复制到 SQL Server 使用的默认备份位置 (C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\Backup),而不是我指定的物理位置在代码中:

 Database database = Server.Databases[dbName]);
  Backup backup = new Backup();

  device = new BackupDevice();
  device.Parent = Server;
  device.Name = dbName + ".bak";
  device.BackupDeviceType = BackupDeviceType.Disk;
  device.PhysicalLocation = Path.Combine(filePath + device.Name); // doesn't appear to do anything
  device.Create();

  backup.Action = BackupActionType.Database;
  backup.Database = database.Name;
  backup.Devices.AddDevice(filePath, DeviceType.File);
  backup.SqlBackup(server);

当我运行我的代码时,我发现我指定的路径(“C:\backupTest”)是空的,并且备份已添加到默认备份位置。

有谁知道这是为什么?

4

1 回答 1

1

试试下面的代码

static void BackupDataBase(string databaseName, string destinationPath)
{
    try
    {
        Server myServer = GetServer();
        Backup backup = new Backup();
        backup.Action = BackupActionType.Database;
        backup.Database = databaseName;
        destinationPath = System.IO.Path.Combine(destinationPath, databaseName + ".bak");
        backup.Devices.Add(new BackupDeviceItem(destinationPath, DeviceType.File));
        backup.Initialize = true;
        backup.Checksum = true;
        backup.ContinueAfterError = true;
        backup.Incremental = false;
        backup.LogTruncation = BackupTruncateLogType.Truncate;
        backup.SqlBackup(myServer);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

private static Server GetServer()
{
    ServerConnection conn = new ServerConnection("server", "username", "pw");
    Server myServer = new Server(conn);
    return myServer;
}

有关更多信息,请参阅此codeproject文章。

于 2013-05-28T17:16:12.573 回答