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.
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.
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:
(Denali was the code name for 2012 before it was released, in case you didn’t know).