Search code examples
c#sql-serversmodatabase-backups

Database collection with smo on sql server 2005, 2008, 2012


I am trying to get a database collection with smo, like this:

var server = GetDatabaseServer();

DatabaseCollection databaseCollection = server.Databases;

foreach (Database database in databaseCollection)
{
    if (String.Equals(database.Name, databseName, 
        StringComparison.OrdinalIgnoreCase))
    {
        if (database.IsSystemObject == false && 
            database.IsMirroringEnabled == false)
        {
            if (IsTimeForBackup(database.LastBackupDate)) 
                Backup(database, server, backupDirectory);
            break;
        }    
    }
}

This will work if the SqlServer is 2005 or 2008. If i try to connect to an instance of 2012, i am getting an exception when i check for database.IsMirroringEnabled:

Index was outside the bounds of the array.

The stack trace is : at Microsoft.SqlServer.Management.Smo.SqlPropertyMetadataProvider.PropertyNameToIDLookupWithException(String propertyName, PropertyAccessPurpose pap) at Microsoft.SqlServer.Management.Smo.PropertyCollection.LookupID(String propertyName, PropertyAccessPurpose pap) at Microsoft.SqlServer.Management.Smo.PropertyCollection.GetValueWithNullReplacement(String propertyName, Boolean throwOnNullValue) at Microsoft.SqlServer.Management.Smo.PropertyCollection.GetValueWithNullReplacement(String propertyName) at Microsoft.SqlServer.Management.Smo.Database.get_IsSystemObject() at RBC.StockManagement.CommunicationService.DatabaseBackup.BackupDatabase(String databseName, String backupDirectory) in D:\Work\Trunk\RBC.StockManagement\RBC.StockManagement.CommunicationService\DatabaseBackup.cs:line 85

The database that i am trying to backup is created from SqlServer 2005 and my operating system runs on x64 architecture. All three instances of 2005(developer edition), 2008 (express edition) and 2012(express edition) are on the same computer.


Solution

  • It looks from this MSDN article that there were new SMO DLLs shipped with SQL Server 2012, and that it won't work if you try to connect to a 2012 instance with the older ones.

    References to SMO dlls in older versions of SQL Server must be removed, and references to the new SMO dlls that are provided with SQL Server 2012 must be included.

    Minimally, you would reference the following:

    • Microsoft.SqlServer.ConnectionInfo

    • Microsoft.SqlServer.Smo

    • Microsoft.SqlServer.Management.Sdk.Sfc

    This related Connect article mentions specifically the error you're getting trying to connect - so I'm guessing this is the same problem. MS says there that they won't update the older ones to allow them to connect to 2012, so I guess updating to the new SMO DLLs is your best shot.

    From regular SO contributer Aaron bertrand:

    See this item for Microsoft's official stance about forward compatibility (not going to fix) and backward compatibility (Denali SSMS will be able to manage 2000, 2005, 2008, 2008 R2): http://connect.microsoft.com/SQLServer/feedback/details/622441

    (Denali was the code name for 2012 before it was released, in case you didn't know).