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

发布于 2024-08-22 02:31:33 字数 2046 浏览 3 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半城柳色半声笛 2024-08-29 02:31:33

不,这永远不会起作用 - SQL Server 只能备份到物理连接到实际 SQL Server 计算机的驱动器。在任何情况下都无法将远程 SQL Server 备份到本地硬盘 - 根本不可能(无论是在 SMO 中还是在 SQL Server Management Studio 中)。

No, this won't ever work - SQL Server can only ever back up to a drive physically attached to the actual SQL Server machine. 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).

好听的两个字的网名 2024-08-29 02:31:33

正如Marc_s所说,这是不可能的。作为一种解决方法,您可以让数据库调用主机中的命令行程序,该程序通过 ftp 发送文件,将其复制到某个共享文件夹或其他位置。

祝你好运

As Marc_s said, It can't be made. 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.

Good Luck

网白 2024-08-29 02:31:33

您可以将远程数据库复制到本地。要复制,请使用以下命令:右键单击 SSMS 中的远程数据库:任务 ->导出数据。在打开的窗口中选择源和目标数据库,它将把所有数据从源(远程)复制到本地数据库。

You can copy remote database to local. To copy use this: Right click on remote database in SSMS: Tasks -> Export data. In opened window choose source and destination database, it will be copy all data from source (remote) to your local database.

权谋诡计 2024-08-29 02:31:33

我知道这是一个旧帖子
但是是的,您可以备份远程 sqlserver 的数据库,

只需在远程和本地计算机上创建相同的文件夹名称和驱动器即可

,例如:

在本地计算机上 d:\sql_backup
d:\sql_backup 在远程计算机上

,它会执行此操作...当然,

如果您想获取备份文件,备份文件将在远程计算机上,只需共享远程计算机的文件夹即可

致敬
鲁本克

I know it is an old post
but Yes you can backup a database of a remote 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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文