Retrieving SQL Server 2005 Database Info Using SMO: Basics - Understanding the SMO connectivity
(Page 4 of 5 )
Let us go through some of the important statements within the above code snippet. The first statement from the code is the following:
Imports Microsoft.SqlServer.Management.Smo
We need the above statement to “import” all classes and other declarations to work with our form. In fact, we need to consider that the “Microsoft.SqlServer.Management.Smo” namespace is the heart of SMO.
Dim svr As Server = New Server()
The above statement creates a new object “svr” based on the class “Microsoft.SqlServerManagement.Smo.Server”. When the object (“svr”) gets created, it automatically tries to connect to the default instance of SQL Server on the OS where our SMO application is running. It tries to connect with “Windows Authentication” by default (also called “Secured Connectivity”).
If you wanted to connect to another instance, you can modify the above statement as follows:
Dim svr As Server = New Server(“192.168.1.2\SQL2K5”)
The above statement tries to connect to the instance named “SQL2K5” existing at “192.168.1.2” (still based on “Windows Authentication”).
If you would like to connect by providing SQL Server authentication, you may need to modify your code as shown here:
Dim svr As Server = New Server(“192.168.1.2\SQL2K5”)
svr.ConnectionContext.LoginSecure = false
svr.ConnectionContext.Login = "username"
svr.ConnectionContext.Password = "password"
Once you connect to a SQL Server instance, you can retrieve information of that instance with the following properties existing in “Microsoft.SqlServer.Management.Smo.Server” class.
- Name
- InstanceName
- Information.Product
- Information.VersionString
- Information.Edition
- Information.Language
Since the above properties are self-explanatory, I shall leave them without any explanation. I don’t want to stop here. We shall further extend our solution to get more important information from the same database instance. Let us proceed to the next section.
Next: How do we retrieve “Database” Information about a particular SQL Server instance using SMO? >>
More MS SQL Server Articles
More By Jagadish Chaterjee