从Windows应用程序备份SQL Server数据库不在SQL Server上托管

时间:2013-07-30 04:02:27

标签: c# sql-server-2008 c#-4.0

有很多可用的示例提供SMO解决方案来备份SQL数据库,我有一个可行的。

我的方案虽然要求我从连接到网络的计算机上的Windows应用程序备份SQL数据库。

问题在于服务器名称...每次运行以下代码时,我都会收到错误“ServerName'的备份失败。”

显然找不到服务器了。当我在服务器(以及我的本地机器上)运行代码时,我的类运行。

如何连接服务器?

我的代码如下:

public void BackupDB(string database, string databasename)
    {

        // Check if file exists - if so delete it...We don't want multiple backups kept.
        if (File.Exists(databasename) == true)
        {
            File.Delete(databasename);
        }

        Server srv = new Server("My SERVER NAME");

        Backup bkpDBFull = new Backup();


        /* Specify whether you want to back up database or files or log */
        bkpDBFull.Action = BackupActionType.Database;

        /* Specify the name of the database to back up */
        bkpDBFull.Database = database;

        /*  backup on the file system */
        bkpDBFull.Devices.AddDevice(databasename, DeviceType.File); 
        bkpDBFull.BackupSetName = "Database Backup";
        bkpDBFull.BackupSetDescription = "Database - Full Backup";

        /* You can specify the expiration date for your backup data
         * after that date backup data would not be relevant */
        //bkpDBFull.ExpirationDate = DateTime.Today.AddDays(10);

        /* You can specify Initialize = false (default) to create a new
         * backup set which will be appended as last backup set on the media. You
         * can specify Initialize = true to make the backup as first set on the
         * medium and to overwrite any other existing backup sets if the all the
         * backup sets have expired and specified backup set name matches with
         * the name on the medium */
        bkpDBFull.Initialize = false;

        /* Wiring up events for progress monitoring */
        progbar1.Value = 0;
        progbar1.Maximum = 100;
        progbar1.Value = 10;
        bkpDBFull.PercentComplete += new PercentCompleteEventHandler(PercentCompleteEventHandler);
        bkpDBFull.PercentCompleteNotification = 10;

        //bkpDBFull.Complete += Backup_Completed;

        /* SqlBackup method starts to take back up
         * You can also use SqlBackupAsync method to perform the backup
         * operation asynchronously */
        try
        {
            bkpDBFull.SqlBackup(srv);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return;
        }

        MessageBox.Show("Backup Complete");

        this.Close();
    }

1 个答案:

答案 0 :(得分:0)

您需要启用SQL Server中其他计算机的连接。本说明将指导您完成整个过程:

http://msdn.microsoft.com/en-us/library/ms345343(v=sql.105).aspx