2009-02-05 70 views

回答

1

備份時間存儲在表msdb.dbo.backupset中。這是一個例程,它需要一個打開的SQL連接,數據庫名稱和一個標誌,指示您是要完整備份還是備份,並返回上次備份的時間。

請注意,此表有時會變得不整齊,因此如果它已被修整,它可能表示沒有備份。

//---------------------------------------------------------------------------------------- 
    // Function: GetLastBackupTime 
    // 
    // Input 
    // sqlConnection   - An open SQLConnection to the target SQL Server 
    // DatabaseName   - Name of the database which you are interested in 
    // fullDatabaseBackupOnly - Do you want only the time of the last full backup 
    // 
    // Output 
    // DateTime    - DateTime.MinValue indicates no backup exists 
    //        otherwise it returns the last backup time 
    //--------------------------------------------------------------------------------------- 

DateTime GetLastBackupTime(SqlConnection sqlConnection, 
          string  databaseName, 
          bool   fullDatabaseBackupOnly) 
{ 
    DateTime lastBackupTime = DateTime.MinValue; 

    string sqlTemplate = "SELECT TOP 1 backup_finish_date " + 
         "FROM msdb.dbo.backupset " + 
         "WHERE database_name='{0}' {1} " 
         "ORDER BY backup_finish_date DESC"; 

    string sql = String.Format(sqlTemplate, 
           databaseName, 
           (fullDatabaseBackupOnly) ? " AND type='D' " : ""); 

    // open connection 
    using (SqlCommand cmd = new SqlCommand(sql, sqlConnection, 
    { 
     object retValue = _Command.ExecuteScalar(); 

     if (retValue != null) lastBackupTime = (DateTime)retValue; 
    } 

    return lastBackupTime; 
} 
0

這是不可能的。