我目前正在使用下面的代码。我不得不增加超时以停止断开连接。数据库只有 1gb,我不知道为什么这个数据库永远不会备份。有什么线索吗?没有错误报告它只是不备份
using Microsoft.SqlServer.Management.Sdk.Sfc;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
public static void BackupDatabase(string backUpFile)
{
string conString = string.Format("Server=server;initial catalog=master;Integrated Security=SSPI;");
ServerConnection con = new ServerConnection(conString);
con.ConnectTimeout = 70000;
con.StatementTimeout = 70000;
Server server = new Server(con);
Backup source = new Backup();
source.Action = BackupActionType.Database;
source.Database = "backedupdb";
BackupDeviceItem destination = new BackupDeviceItem(backUpFile+"\\"+source.Database.ToString(), DeviceType.File);
source.Devices.Add(destination);
source.Initialize = false;
source.PercentComplete += CompletionStatusInPercent;
source.Complete += Backup_Completed;
source.SqlBackup(server);
con.Disconnect();
}
private static void CompletionStatusInPercent(object sender, PercentCompleteEventArgs args)
{
Console.Clear();
Console.WriteLine("Percent completed: {0}%.", args.Percent);
}
private static void Backup_Completed(object sender, ServerMessageEventArgs args)
{
Console.WriteLine("Hurray...Backup completed.");
Console.WriteLine(args.Error.Message);
}
private static void Restore_Completed(object sender, ServerMessageEventArgs args)
{
Console.WriteLine("Hurray...Restore completed.");
Console.WriteLine(args.Error.Message);
}