Thursday, May 21, 2009

Opening a SQL file in Visual Studio 2008 at the click of a button

I love the SQL interface that is built into Visual Studio 2008. It has much of the key functionality that Microsoft SQL Server Management Studio has.

The problem is that it is not a docked window. It is a regular window just like any other window in Visual Studio 2008. This makes it difficult to find it in my often long list of Visual Studio windows that are open.

What I wanted was a way to see my SQL file at the click of the button. Then I thought, why not have it open for me if it is not one of the windows that is already open in Visual Studio 2008.

Macros (aka Visual Basic for Applications (VBA)) is a natural way to add this functionality. I explain where Macros are hiding in Visual Studio 2008 and how to record a macro in another one of my blog entries.

Here is the code snippet you can use to do what I describe above. Obviously, you will need to change your paths to suit your situation.

Sub GotoSQLWindow()
            DTE.Windows.Item("(local).MyDB - dev.sql").Activate()
        Catch ex As Exception
            Catch ex2 As Exception
                Throw ex2
            End Try
        End Try
    End Sub

I recommend if you have trouble getting the names correctly, you either look at the tabs and put that exact text as the name in the Item collection, or you record it using Control-Shift-R.

No comments: