Thursday, May 18, 2006 - Posts

Attention to Detail when using SMO

In a my post Execute SQL Scripts against multiple DB's with SMO I demonstrated a way to execute a script against multiple databases. I then created snapshots on all those databases and found a new "bug" in my script processing code.

Duh! You can't alter a Snapshot database! Of course I knew that, but I didn't make provisions in my multi-database script processing code to skip snapshot databases. Here's the updated code:

        ' Browse the database collection on the target server
        Dim lisDBs As DatabaseCollection
        lisDBs = srvMgmtServer.Databases
        Dim objDB As Database
        For Each objDB In lisDBs
            If Not objDB.IsDatabaseSnapshot Then
                If Left(objDB.Name, 4) = "xxx_" Or Left(objDB.Name, 4) = "yyy_" Then
                    objDB.ExecuteNonQuery(allText)
                End If
            End If
        Next

Notice the check against the IsDatabaseSnapshot property before doing any work against that database. These are the kind of features I really enjoy while working with SMO.

Allen