2009-02-05 67 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

这是不可能的。