简体   繁体   English

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

[英]C# SMO backup of remote database to local machine

I have an application which performs backups and restores of SQL databases, this works fine on the local machine, however if I run this against a SQL server hosted on another machine I get the following error我有一个执行 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.).

This appears to be being caused by the SQL server attempting to write this file to its local drive.这似乎是由 SQL 服务器试图将此文件写入其本地驱动器引起的。 I cannot setup a shared area into which the backup can be placed due to security restrictions.由于安全限制,我无法设置可以放置备份的共享区域。

Does anyone know how I can move this data back to the machine the code is being called from?有谁知道如何将这些数据移回正在调用代码的机器?

My code is below.我的代码如下。

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;
    }
}

No, this won't ever work - SQL Server can only ever back up to a drive physically attached to the actual SQL Server machine.不,这永远行不通 - SQL Server 只能备份到物理连接到实际 SQL Server 机器的驱动器。 You cannot under any circumstances back up a remote SQL Server to your local harddisk - just not possible (neither in SMO, or in SQL Server Management Studio).在任何情况下都不能将远程 SQL Server 备份到本地硬盘 - 只是不可能(无论是在 SMO 中,还是在 SQL Server Management Studio 中)。

You can copy remote database to local.您可以将远程数据库复制到本地。 To copy use this: Right click on remote database in SSMS: Tasks -> Export data.要复制使用此:右键单击 SSMS 中的远程数据库:任务 -> 导出数据。 In opened window choose source and destination database, it will be copy all data from source (remote) to your local database.在打开的窗口中选择源和目标数据库,它会将所有数据从源(远程)复制到本地数据库。

As Marc_s said, It can't be made.正如 Marc_s 所说,它无法制造。 As a workarround you make that your database call a command line program in the host, that send the file by ftp, copy it to some shared folder, or something else.作为一种解决方法,您可以让数据库调用主机中的命令行程序,通过 ftp 发送文件,将其复制到某个共享文件夹或其他地方。

Good Luck祝你好运

I know it is an old post but Yes you can backup a database of a remote sqlserver我知道这是一篇旧帖子,但是您可以备份远程 sqlserver 的数据库

you just create the same folder name and drive in both the remote and local computer您只需在远程和本地计算机中创建相同的文件夹名称和驱动器

example:例子:

d:\\sql_backup on local computer d:\\sql_backup on remote computer d:\\sql_backup在本地计算机上d:\\sql_backup在远程计算机上

and it will do it... of course the backup file will be on the remote computer它会做到的......当然备份文件将在远程计算机上

if you want to get the backup files, just share the folder of the remote computer如果要获取备份文件,只需共享远程计算机的文件夹即可

saludos rubenc萨鲁多斯鲁本克

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从本地计算机备份远程MySQL数据库 - Backup remote MySQL database from local machine 如何使用 C# 中的 SMO 使用文件 STREAM 备份和恢复数据库 - How To Backup And Restore DataBase With FILE STREAM Using SMO in C# 在C#中使用SMO备份SQL Server数据库 - Using SMO in C# to backup SQL Server database c#SMO备份-如何使用ExpirationDate? - c# SMO Backup - how to use ExpirationDate? 通过C#代码从备份文件还原远程计算机上的MSSQL数据库 - Restore MSSQL database on remote machine from backup file via C# code 使用SMO备份带有C#Windows Forms应用程序的SQL Server 2005数据库时,备份失败 - Using SMO to backup SQL Server 2005 Database with C# Windows Forms Application, Back up failed 如何在不使用SMO的情况下在C#中备份数据库(SQL Server 2008)? - How to backup database (SQL Server 2008) in C# without using SMO? C# SQL Server 将远程数据库备份到远程默认备份位置,无需直接访问远程位置? - C# SQL Server backup of a remote database to the remote default backup location without direct access to the remote location? C#SMO数据库不记录创建 - C# SMO Database do not log creation C#SMO Executenonquery数据库失败 - C# SMO Executenonquery failed for database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM