通过编码创建数据库备份 [英] Creating backup of database by coding
问题描述
我编写代码来创建SQL Server 2005数据库备份并再次恢复它。
它运行正常但显示有些错误。
我的代码如下: -
I wrote codes to create Backup of SQL Server 2005 database and to restore it again.
It runs properly but showing some error.
My code is as follows:-
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Server myServer = new Server(@"MASHKOOR-PC");
try
{
myServer.ConnectionContext.LoginSecure = false;
myServer.ConnectionContext.Login = "sa";
myServer.ConnectionContext.Password = "md22mashkoor";
myServer.ConnectionContext.Connect();
Database myDatabase = myServer.Databases["Kolkata_Dental"];
BackupDatabaseFull(myServer, myDatabase);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.InnerException.Message);
}
finally
{
if (myServer.ConnectionContext.IsOpen)
myServer.ConnectionContext.Disconnect();
Console.WriteLine("Press any key to terminate...");
Console.ReadKey();
}
}
private static void BackupDatabaseFull(Server myServer, Database myDatabase)
{
Backup bkpDBFull = new Backup();
bkpDBFull.Action = BackupActionType.Database;
bkpDBFull.Database = myDatabase.Name;
bkpDBFull.Devices.AddDevice(@"D:\KolkataDentalFull.bak", DeviceType.File);
bkpDBFull.BackupSetName = "KolkataDental database Backup";
bkpDBFull.BackupSetDescription = "KolkataDental database - Full Backup";
bkpDBFull.ExpirationDate = DateTime.Today.AddDays(1000);
bkpDBFull.Initialize = false;
bkpDBFull.PercentComplete += CompletionStatusInPercent;
bkpDBFull.Complete += Backup_Completed;
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);
}
private static void RestoreDatabase(Server myServer, Database myDatabase)
{
Restore restoreDB = new Restore();
restoreDB.Database = myDatabase.Name;
/* Specify whether you want to restore database or files or log etc */
restoreDB.Action = RestoreActionType.Database;
restoreDB.Devices.AddDevice(@"D:\KolkataDentalFull.bak", DeviceType.File);
/* You can specify ReplaceDatabase = false (default) to not create a new image *
* of the database, the specified database must exist on SQL Server instance. *
* If you can specify ReplaceDatabase = true to create new database image *
* regardless of the existence of specified database with same name */
restoreDB.ReplaceDatabase = true;
/* If you have differential or log restore to be followed, you would need *
* to specify NoRecovery = true, this will ensure no recovery is done after the *
* restore and subsequent restores are allowed. It means it will database * in the Restoring state. */
restoreDB.NoRecovery = true;
/* Wiring up events for progress monitoring */
restoreDB.PercentComplete += CompletionStatusInPercent;
restoreDB.Complete += Restore_Completed;
/* SqlRestore method starts to restore database * You cab also use SqlRestoreAsync method to perform restore * operation asynchronously */
restoreDB.SqlRestore(myServer);
}
private static void Restore_Completed(object sender, ServerMessageEventArgs args)
{
Console.WriteLine("Hurray...Restore completed.");
Console.WriteLine(args.Error.Message);
}
}
}
显示错误如下: -
服务器'MASHKOOR-PC'备份失败。
无法加载文件或程序集'Microsoft.SqlServer.BatchParser,Version = 9.0.242.0,Culture = neutral,PublicKeyToken = 89845dcd8080cc91'或其依赖项之一。试图加载格式不正确的程序。
我无法找出原因。请帮助我。
It showed error is as follows:-
Backup failed for Server 'MASHKOOR-PC'.
Could not load file or assembly 'Microsoft.SqlServer.BatchParser, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. An attempt was made to load a program with an incorrect format.
I am unable to find out the reason. Kindly help me.
推荐答案
您需要为SQL Server 2005安装64位组件。
您可以从此处下载。 [ ^ ]
错误是因为您正在运行64位进程并尝试加载x86 DLL。您还可以尝试将应用程序重新编译为x86(Properties-> Build)并再次运行它。您现在可能已将其设置为AnyCPU。
You need to install the 64-bit components for SQL Server 2005.
You can download them from here.[^]
The error is because you are running a 64 bit process and trying to load an x86 DLL. You can also try recompiling your application to be x86 (Properties->Build) and run it again. You probably have it set to AnyCPU right now.
您需要安装Microsoft SQL Server 2005管理对象集合,它应该能够解决问题。
从此链接下载并安装。
http://download.microsoft.com/download/4/4/D/44DBDE61-B385-4FC2-A67D-48053B8F9FAD/SQLServer2005_XMO_x64.msi
谢谢
--RA
You need to install the Microsoft SQL Server 2005 Management Objects Collection, it should be able to solve the problem.
Download from this link and install.
http://download.microsoft.com/download/4/4/D/44DBDE61-B385-4FC2-A67D-48053B8F9FAD/SQLServer2005_XMO_x64.msi
Thanks
--RA
这篇关于通过编码创建数据库备份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!