Can We Manage SQL Server 2000 With WMI and Visual Basic.NET?
(Page 1 of 5 )
The previous article mainly concentrated on fetching information available in SQL Server 2000. This article explains how to manage SQL Server 2000 using WMI together with Visual Basic.NET language.
A downloadable file for this article is available
here.
The sample downloadable solution (zip) is entirely developed using Visual Studio.NET 2003 Enterprise Architect on Windows Server 2003 Standard Edition together with SQL Server 2000 Enterprise Edition. But, I am confident that it would work with other versions of Windows (which support .NET 1.1) versions as well.
Is SQL Server 2000 service started on your computer?
I strongly suggest you refer to my previous article “Can we integrate Visual Basic.NET, SQL Server 2000 and WMI?” before proceeding through this article. In my previous article, I mainly focused on retrieving SQL Server database objects using WMI. Now, in this article, I shall introduce you to managing a SQL Server database using WMI together with Visual Basic .NET.
Before managing the database, generally every developer would check whether the instance of SQL Server started running or not. Everyone knows how to check it using the Service Manager available in SQL Server. Now we shall achieve almost the same thing using the following code:
Private Sub btnStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStatus.Click
Dim PrivateLateBoundObject As ManagementObject
'PrivateLateBoundObject = New ManagementObject(Nothing, New ManagementPath("\\SERVER\root\CIMV2:
Win32_Service.Name=""MSSQLServer"""), Nothing)
PrivateLateBoundObject = New ManagementObject(Nothing, New ManagementPath("\\SERVER\root\MicrosoftSQLServer:
Win32_Service.Name=""MSSQLSERVER"""), Nothing)
MessageBox.Show(CType(PrivateLateBoundObject("Started"), Boolean))
PrivateLateBoundObject.Dispose()
End Sub
From the above code, you can understand that I am creating an object (PrivateLateBoundObject) based on the “ManagementObject” class. The “ManagementPath” starts with “\\yourhostname”. There exists a WMI class called “Win32_Service” within the “MicrosoftSQLServer” namespace (which comes onto the scene after you installed the “WMI SQL Server Administration Provider”).
In another scenario, you need not use only the “MicrsoftSQLServer” namespace in WMI; you can directly use “CIMV2” as well (as commented in the coding). If you don’t install “WMI SQL Server Administration Provider” (as specified in my previous article), you will not be able to work with the “MicrosoftSQLServer” namespace. But you would still be able to work with “CIMV2”, which comes with default installation of the Windows OS.
There exists a WMI property called “Started” (of type Boolean) which is used to check the status of the Microsoft SQL Server 2000 service. It returns “true” if the service is running (or started) or else returns “false” if it is not.
Next: How do you “start” or “stop” the MS SQL Server 2000 service? >>
More MS SQL Server Articles
More By Jagadish Chaterjee