我想使用C#代码创建特定数据库的备份,为此我使用以下代码。
代码
Console.WriteLine("Backup manager");
Backup bkpDBFull = new Backup();
bkpDBFull.Action = BackupActionType.Database;
bkpDBFull.Database = "SampleDB";
bkpDBFull.Devices.AddDevice(@"D:\Temp\Back\fulBak.bak", DeviceType.File);
bkpDBFull.BackupSetName = "Database backup";
bkpDBFull.BackupSetDescription = "Code backup";
bkpDBFull.Initialize = false;
bkpDBFull.PercentComplete += CompletionStatusInPercent;
bkpDBFull.Complete += Backup_Completed;
Server MyServer = new Server("Data Source=.;Initial Catalog=SampleDB;User ID=user;Password=pass");
bkpDBFull.SqlBackup(MyServer);
和两个事件的以下。
完成状态百分比
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);
}
在整个代码中,它在下面的代码中只有一个问题
问题代码
Server MyServer = new Server("Data Source=.;Initial Catalog=SampleDB;User ID=user;Password=pass");
我想我以错误的方式输入了 sqlserver 的名称。
我需要如何解决此问题的解决方案。