C#源码 备份和恢复数据库
SQLDMO(SQL Distributed Management Objects,SQL分布式管理对象)封装了Microsoft SQL Server数据库中的对象。SQLDMO是Microsoft SQL Server中企业管理器所使用的应用程序接口,所以它可以执行很多功能,其中当然也包括对数据库的备份和恢复。
当然要用到 sqldmo 来做这个工作
找到 sqldmo.dll这个文件
C:\Program Files\Microsoft SQL Server\80\Tools\Binn\sqldmo.dll
用.Net框架提供的工具转成受托管代码
tlbimp sqldmo.dll /out: c:\SQLDMO.dll
1using System;
2
3namespace DbService
4{
5 /// <summary>
6 /// DbOper类,主要应用SQLDMO实现对Microsoft SQL Server数据库的备份和恢复
7 /// </summary>
8 public sealed class DbOper
9 {
10 /// <summary>
11 /// DbOper类的构造函数
12 /// </summary>
13 private DbOper()
14 {
15 }
16
17 /// <summary>
18 /// 数据库备份
19 /// </summary>
20 public static void DbBackup()
21 {
22 SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
23 SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
24 try
25 {
26 oSQLServer.LoginSecure = false;
27 oSQLServer.Connect("localhost", "sa", "1234");
28 oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
29 oBackup.Database = "Northwind";
30 oBackup.Files = @"d:\Northwind.bak";
31 oBackup.BackupSetName = "Northwind";
32 oBackup.BackupSetDescription = "数据库备份";
33 oBackup.Initialize = true;
34 oBackup.SQLBackup(oSQLServer);
35 }
36 catch
37 {
38 throw;
39 }
40 finally
41 {
42 oSQLServer.DisConnect();
43 }
44 }
45
46 /// <summary>
47 /// 数据库恢复
48 /// </summary>
49 public static void DbRestore()
50 {
51 SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
52 SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
53 try
54 {
55 oSQLServer.LoginSecure = false;
56 oSQLServer.Connect("localhost", "sa", "1234");
57 oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
58 oRestore.Database = "Northwind";
59 oRestore.Files = @"d:\Northwind.bak";
60 oRestore.FileNumber = 1;
61 oRestore.ReplaceDatabase = true;
62 oRestore.SQLRestore(oSQLServer);
63 }
64 catch
65 {
66 throw;
67 }
68 finally
69 {
70 oSQLServer.DisConnect();
71 }
72 }
73 }
74}
75
76
2
3namespace DbService
4{
5 /// <summary>
6 /// DbOper类,主要应用SQLDMO实现对Microsoft SQL Server数据库的备份和恢复
7 /// </summary>
8 public sealed class DbOper
9 {
10 /// <summary>
11 /// DbOper类的构造函数
12 /// </summary>
13 private DbOper()
14 {
15 }
16
17 /// <summary>
18 /// 数据库备份
19 /// </summary>
20 public static void DbBackup()
21 {
22 SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
23 SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
24 try
25 {
26 oSQLServer.LoginSecure = false;
27 oSQLServer.Connect("localhost", "sa", "1234");
28 oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
29 oBackup.Database = "Northwind";
30 oBackup.Files = @"d:\Northwind.bak";
31 oBackup.BackupSetName = "Northwind";
32 oBackup.BackupSetDescription = "数据库备份";
33 oBackup.Initialize = true;
34 oBackup.SQLBackup(oSQLServer);
35 }
36 catch
37 {
38 throw;
39 }
40 finally
41 {
42 oSQLServer.DisConnect();
43 }
44 }
45
46 /// <summary>
47 /// 数据库恢复
48 /// </summary>
49 public static void DbRestore()
50 {
51 SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
52 SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
53 try
54 {
55 oSQLServer.LoginSecure = false;
56 oSQLServer.Connect("localhost", "sa", "1234");
57 oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
58 oRestore.Database = "Northwind";
59 oRestore.Files = @"d:\Northwind.bak";
60 oRestore.FileNumber = 1;
61 oRestore.ReplaceDatabase = true;
62 oRestore.SQLRestore(oSQLServer);
63 }
64 catch
65 {
66 throw;
67 }
68 finally
69 {
70 oSQLServer.DisConnect();
71 }
72 }
73 }
74}
75
76