使用Smo.Backup备份SQL Server数据库字符串字符串、备份、数据库、Smo

2023-09-04 03:05:51 作者:思念吃了炫迈根本停不下来

我试图让一个小应用程序,这将有助于我让我的服务器备份。该应用程序将我的家用电脑上运行,因此主要目标是能够连接到外部服务器,备份选定的数据库,转储备份内容为一个字符串或东西,所以我可以把它写在我的电脑磁盘,而不是服务器的磁盘。

I'm trying to make a little app that would help me making my server backup. That app would run on my home PC so the main goal is to be able to connect to the external server, backup the selected database, dump the backup content to a string or something so I could write it on my PC disk and not the server's disk.

我这样做,其工作是在服务器的磁盘上写的,但我希望能够在我的电脑的磁盘写入备份的结果。

I did that which works to write on the server's disk, but I'd like to be able to write on my PC's disk the backup's result.

private bool BackupDatabase()
{
    try
    {
        // Filename
        string sFileName = string.Format("{0}\\{1}.bak", _sWhereToBackup, DatabaseName);

        // Connection
        string sConnectionString = String.Format(
            "Data Source=tcp:{0};Initial Catalog={1};User ID={2};Pwd={3};",
            DatabaseHost, DatabaseName, DatabaseUsername, DatabasePassword);

        SqlConnection oSqlConnection = new SqlConnection(sConnectionString);
        Server oServer = new Server(new ServerConnection(oSqlConnection));

        // Backup
        Backup backup = new Backup();
        backup.Action = BackupActionType.Database;
        backup.Database = DatabaseName;
        backup.Incremental = false;
        backup.Initialize = true;
        backup.LogTruncation = BackupTruncateLogType.Truncate;

        // Backup Device
        BackupDeviceItem backupItemDevice = new BackupDeviceItem(sFileName, DeviceType.File);
        backup.Devices.Add(backupItemDevice);

        // Start Backup
        backup.SqlBackup(oServer);
    }
    catch (Exception ex)
    {
        throw ex;
    }

    return false;
}

太感谢了!

推荐答案

这是会得到一个有点哈克,因为你需要为

This is going to get a bit hacky because you need to either

在调用SQL函数来读取文件的服务器上,并返回一个二进制数组给你,然后再转换回一个文件中。这将需要适当的权限,你正在下运行访问服务器的驱动器上的文件的帐户。

您可以使用T-SQL或更多的高级.NET code T-SQL可以这个伟大的sql注入指南中可以看出 http://www.blackhat.com/$p$psentations/bh-europe-09/Guimaraes/Blackhat-europe-09-Damele-SQLInjection-slides.pdf

you can use t-sql or a bit more 'advanced' .net code t-sql can be seen in this great sql injection guide http://www.blackhat.com/presentations/bh-europe-09/Guimaraes/Blackhat-europe-09-Damele-SQLInjection-slides.pdf

或.net: http://www.mssqltips.com/sqlservertip/2349/read-and-write-binary-files-with-the-sql-server-clr/

映射文件位置(即访问共享/驱动器/ IPC连接)在网络上

map a file location (i.e. access a share/drive/ipc connection) over the network

在服务器的ftp文件到一个位置

have the server ftp the files to a location

这在您的方案听起来很容易?

Which sounds likely in your scenario?