Saturday, October 30, 2010

SQL Server - Verifying backups using SMO



In order to perform a verify using SMO we require two objects, a Server object and a Restore object. In its simplest form, a Restore object requires only a few properties to be set before calling the SqlVerify method and passing in the Server object as can be seen in the following example that does a verify of a database backup from the file c:\SMOTest.bak. This example uses the overloaded SqlVerify method to return the verify error if the method returns false.

using System;
using Microsoft.SqlServer.Management.Smo;

namespace SMOTest
{
    class Program
    {
        static void Main()
        {
            Server svr = new Server();
            Restore res = new Restore();
            Boolean verified;
            String errormsg;

            res.Devices.AddDevice(@"C:\SMOTest.bak", DeviceType.File);

            verified = res.SqlVerify(svr, out errormsg);

            Console.WriteLine("verified = " + verified.ToString());

            if (verified == false)
                Console.WriteLine(errormsg);

        }
    }
}

No comments:

Post a Comment