Making an SMO Connection in .NET Whidbey / Yukon
I've been pulling my hair out for about 10 days trying to understand SQL Management Objects in .NET Whidbey / Yukon. My problem has not been the object model. My problem has been that I've been unable to make a connection to the database server. I have tried and tried and tried with no success, each time getting a connection failure type of error message. I figured out my problem this evening.
Here was the code that did not work:
Dim strCn As String = "........."
Dim sqlServerCn As New ServerConnection(strCn)
Dim objServer As New Server(sqlServerCn)
MessageBox.Show(objServer.Information.OSVersion.ToString())
The problem was that in looking at the overloads for the ServerConnection() object, I had assumed that because there was a SqlConnection object that could be passed in to initialize the ServerConnection, that the object would also take a string with the connection string embedded. Wow, was I wrong. Here is what will work:
Dim strCn As String = ".........."
Dim sqlCn As New SqlConnection(strCn)
Dim sqlServerCn As New ServerConnection(sqlCn)
Dim objServer As New Server(sqlServerCn)
MessageBox.Show(objServer.Information.OSVersion.ToString())
Obviously, this is written in Visual Basic .NET. I have tested this and I am getting back good information using .NET Whidbey Beta 1, Sql Server 2005 Beta 2, and a Virtual PC Session running WindowsXP.
Happy Coding..........