SQL Server备份从不完成或报告任何百分比
本文关键字:报告 任何 百分比 Server 备份 SQL | 更新日期: 2023-09-27 18:12:25
我目前使用下面的代码。为了停止断开连接,我不得不增加超时时间。数据库只有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);
}
连接字符串是上面例子中的问题,它不需要一个"connectionstring",它只是想要服务器名称。连接不需要初始化,它将自动被调用。
string conString = string.Format("server");
ServerConnection con = new ServerConnection(conString);