【发布时间】:2013-07-30 04:02:27
【问题描述】:
有许多示例可以提供 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();
}
【问题讨论】:
-
我看不到你定义的哪个文件备份应该去哪里!另外:如果您在远程服务器上运行 - 请注意,备份文件将被放置 该远程服务器的文件系统 - 不到您的本地PC的磁盘!您需要注意这一点,并确保您在该远程服务器的文件系统上定义的路径存在,并且您运行此代码的身份可以写入该位置 ....
标签: c# sql-server-2008 c#-4.0