如何:创建可从快照还原的数据库类

上次修改时间: 2010年1月13日

适用范围: SharePoint Foundation 2010

如果您拥有承载在 Microsoft SQL Server 的 Enterprise 或 Developer 版本中的自定义数据库组件,并且希望通过 Microsoft SharePoint Foundation UI 或对象模型从快照还原该组件,则必须使用实现 IDatabaseSnapshotRestore 接口的类来表示该组件。本主题介绍如何实现这一目的。

警告注释警告

快照要求数据库必须正在运行,因此快照不能是常规目的的数据库备份。因此,在实际应用场合,您的类还必须实现 IBackupRestore 接口。该主题据此假设您已执行如何:创建可以备份和还原的内容类中的过程,并且用于实现 IDatabaseSnapshotRestore 的类与您在后面的主题中创建的类相同。

实现 IDatabaseSnapshotRestore 的成员

  1. 实现 OnPreRestore(SPDatabaseSnapshotRestoreEvent) 方法。提供在从快照还原数据库之前需要执行的任何自定义逻辑。

    重要注释重要信息

    如果您的实现不能满足还原所必需的先决条件,则它肯定会 引发异常。有关此要求的详细信息,请参阅有关 SPDatabaseSnapshot.Restore() 重载的参考主题。

    以下示例演示了使数据库脱机、然后在将线程暂停 5 秒钟后再继续还原的实现。在本例中,"this"是指名为 SupplementalDatabase 的自定义类型的对象,该类型是开发人员从 SPDatabase 类派生的。

    public void OnPreRestore(SPDatabaseSnapshotRestoreEvent args)
    {
        if (args == null)
        {
            throw new ArgumentNullException("args");
        }
    
        try
        {
            this.Status = SPObjectStatus.Offline;
            this.Update();
            Thread.Sleep(5000);
        }
        catch (exception)
        {
            Exception e = new Exception(String.Format("Restoration from {0} cancelled because pre-restoration steps could not be executed.", args.Snapshot.Name), exception);
            throw e;
        }
    }
    
    Public Sub OnPreRestore(ByVal args As SPDatabaseSnapshotRestoreEvent)
            If args Is Nothing Then
                    Throw New ArgumentNullException("args")
            End If
    
            Try
                    Me.Status = SPObjectStatus.Offline
                    Me.Update()
                    Thread.Sleep(5000)
            Catch e1 As exception
                    Dim e As New Exception(String.Format("Restoration from {0} cancelled because pre-restoration steps could not be executed.", args.Snapshot.Name), exception)
                    Throw e
            End Try
    End Sub
    
  2. 实现 OnPostRestore(SPDatabaseSnapshotRestoreEvent) 方法。提供在从快照还原数据库后需要执行的任何自定义逻辑。以下示例演示了将数据库重新设置为联机的实现。在本例中,"this"是指名为 SupplementalDatabase 的自定义类型的对象,该类型是开发人员从 SPDatabase 类派生的。

    public void OnPostRestore(SPDatabaseSnapshotRestoreEvent args)
    {
        if (args == null)
        {
            throw new ArgumentNullException("args");
        }
    
        this.Status = SPObjectStatus.Online;
        this.Update(true);
    }
    
    Public Sub OnPostRestore(ByVal args As SPDatabaseSnapshotRestoreEvent)
            If args Is Nothing Then
                    Throw New ArgumentNullException("args")
            End If
    
            Me.Status = SPObjectStatus.Online
            Me.Update(True)
    End Sub
    

    备注

    您的自定义类将使用不同的签名实现两种 OnPostRestore 方法;第一种方法来自 IBackupRestore,第二种方法来自 IDatabaseSnapshotRestore

请参阅

任务

如何:创建可以备份和还原的内容类

引用

IBackupRestore

IDatabaseSnapshotRestore

SPDatabaseSnapshot.Restore()

SPDatabase