日期:2011-08-29  浏览次数:20384 次

我们知道,用SQLDMO可以实现对数据库的备份与恢复,下面给出简单的实现方法。
首先需要添加对SQLDMO引用

1.实现数据库的备份:
 1/**//// <summary>
 2        /// 数据库备份
 3        /// </summary>
 4        /// <returns>备份是否成功</returns>
 5        public bool DbBackup()
 6        {
 7            string path = CreatePath();
 8            SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
 9            SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
10            try
11            {
12                oSQLServer.LoginSecure = false;
13                oSQLServer.Connect(server,uid, pwd);
14                oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
15                oBackup.Database = database;
16                oBackup.Files = path;
17                oBackup.BackupSetName = database;
18                oBackup.BackupSetDescription = "数据库备份";
19                oBackup.Initialize = true;
20                oBackup.SQLBackup(oSQLServer);
21
22                return true;
23            }
24            catch(Exception ex)
25            {
26                return false;
27                throw ex;
28            }
29            finally
30            {
31                oSQLServer.DisConnect();
32            }
33        }
2.实现数据库恢复:
在恢复时要注意先杀掉当前数据库的所有进程
 1/**//// <summary>
 2        /// 数据库恢复
 3        /// </summary>
 4        public string DbRestore()
 5        {
 6            if(exepro()!=true)//执行存储过程
 7            {
 8                return "操作失败";
 9   &nb