System.Data.SqlClient.SqlException
  HResult=0x80131904
  Message=Exclusive access could not be obtained because the database is in use.
RESTORE DATABASE is terminating abnormally.

  Source=.Net SqlClient Data Provider
  StackTrace:
   在 System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
刚开始使用杀掉数据库活动链接的方式无效,后来尝试在还原之前,让数据库离线的方式,可以成功,
示例代码如下:
    cmd.CommandType = CommandType.Text
    cmd.CommandText = "USE master"
    cmd.CommandText &= vbCrLf 
    cmd.CommandText &= $"ALTER DATABASE [{pRestoreObjectName}] SET offline with rollback immediate"
    cmd.CommandText &= vbCrLf 
    cmd.CommandText &= "RESTORE DATABASE [" & pRestoreObjectName & "]"
    cmd.CommandText &= " FROM DISK= '" & pBackupFile & "'"
    cmd.CommandText &= vbCrLf 
    cmd.CommandText &= $"ALTER DATABASE [{pRestoreObjectName}] SET online with rollback immediate"
    cmd.ExecuteNonQuery()