I have a C# windows Forms application and when the start-up Form loads, i want to check the last time a backup was performed on the database the application connects to.
2 Answers
you could create a store procedure that does the work for you. then you can execute it within the form's OnLoad event or where ever it fits for your need.
Have a look at the below for getting the T-SQL that does the trick
Comments
These are taken from my automation of backups:
First get a list of all databases, including their database GUID:
select db.name, db.database_id, rec.database_guid
from sys.databases db
inner join sys.database_recovery_status rec on db.database_id = rec.database_id
where db.source_database_id is null and db.name <> 'tempdb'
The condition on source_database_id excludes snapshots.
Then using the GUID from the above, get the date of the last full backup type='D' that isn't COPY_ONLY:
SELECT MAX(backup_finish_date) as backup_finish_date
from msdb..backupset
where type='D' and database_guid = @DbGuid and is_copy_only=0