有谁知道如何使用SMO查找数据库的最新备份?
答案 0 :(得分:1)
备份时间存储在表msdb.dbo.backupset中。这是一个例程,它接受一个打开的SQLconnection,databasename和一个标志,指示您是要完整备份还是任何备份,并返回上次备份的时间。
请注意,此表有时会被修整,因此如果已经整理过,它可能表示没有备份。
//----------------------------------------------------------------------------------------
// 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;
}
答案 1 :(得分:0)
这是不可能的。