我创建了一个备份数据库的类,代码是:
public bool BackupDatabase(string databasename)
{
bool success = false;
try
{
Backup dbBackup = new Backup();
string SqlInstance = @"SERVER\INSTANCE";
string User = ExtractPureUsername(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
string BackupLocation = @"\\SERVER\FOLDER\BACKUPTEST_" + User.ToString() + ".bak";
Server srv = new Server(SqlInstance);
dbBackup.Action = BackupActionType.Database;
dbBackup.Database = databasename;
dbBackup.Devices.AddDevice(BackupLocation, DeviceType.File);
dbBackup.BackupSetName = "Test database backup";
dbBackup.ExpirationDate = DateTime.Today.AddDays(10);
dbBackup.Initialize = false;
dbBackup.PercentComplete += CompletionStatusInPercent;
dbBackup.Complete += Backup_Completed;
dbBackup.SqlBackup(srv);
success = true;
}
catch(Exception ex)
{
Console.WriteLine(ex);
Console.ReadKey();
}
return success;
}
这会贯穿,我收到错误:
无法打开备份设备 - 操作系统错误5
如果我要在引用的实例上运行备份,在SSMS中,我可以备份到指定的Bakup位置。所以我假设错误正在发生,因为备份是作为运行C#程序的用户启动的,并且SQL服务器没有它。有没有办法指定运行备份的用户?
答案 0 :(得分:1)
您可能必须在SQL Server计算机上将正确的文件名设置为本地路径。例如,使用\\ServerName\Whatever
而不是c:\Whatever
。确保您生成的文件名不包含非法字符,例如\
或其他内容。