数据库备份和恢复数据库再续

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  {
         SQLDMO.Restore restore = new SQLDMO.RestoreClass();
        SQLDMO.SQLServer sqlServer = new SQLDMO.SQLServerClass();
        try
        {
            sqlServer.LoginSecure = false;
            sqlServer.Connect(Environment.MachineName, "sa", "zzb");
            string strSQL = "select spid from master..sysprocesses where dbid=db_id('" + ConfigurationManager.AppSettings["DBName"] + "')";
            SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
            SqlDataAdapter Da = new SqlDataAdapter(strSQL, conn);
            DataTable spidTable = new DataTable();
            Da.Fill(spidTable);
            for (int iRow = 0; iRow <= spidTable.Rows.Count - 1; iRow++)
            {
                sqlServer.KillProcess(int.Parse(spidTable.Rows[iRow][0].ToString()));
            }
            restore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
            restore.Database = ConfigurationManager.AppSettings["DBName"];
            restore.Files = Server.MapPath(".." + ConfigurationManager.AppSettings["BackPath"]) + backPath.Text.Trim().ToString();
            restore.FileNumber = 1;
            restore.ReplaceDatabase = true;
            restore.SQLRestore(sqlServer);
            Response.Write("<script langauage='javascript'>alert('还原数据库成功')</script>");
        }
        catch (Exception ex)
        {
            Lemon.DBUtility.HelperSQL.ShowMsgBox(ex.Message, this.Page);
        }
        finally
        {
            sqlServer.DisConnect();
        }
    }

51  
 }

}
处理了数据库不能还原,独占排它问题

你可能感兴趣的:(数据库备份)