-2

有许多示例可以提供 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();
    }
4

1 回答 1

0

您需要在 SQL Server 中启用来自其他计算机的连接。此描述将指导您完成整个过程:

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

于 2013-07-30T04:40:21.540 回答