有人知道如何使用SMO查找数据库的最新备份吗?
发布于 2009-02-16 22:23:36
备份时间存储在表msdb.dbo.backupset中。以下是一个例程,它接受一个打开的SQLconnection、数据库名和一个指示您想要完整备份还是任何备份的标志,并返回上次备份的时间。
请注意,这个表有时会被整理,因此如果它已经整理好了,它可能会指示没有备份。
//----------------------------------------------------------------------------------------
// 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;
}发布于 2009-07-02 16:52:01
这不可能。
https://stackoverflow.com/questions/518105
复制相似问题