SunQuest
 
       Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 3 - Can We Integrate Visual Basic.NET, SQL Ser...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Can We Integrate Visual Basic.NET, SQL Server 2000 and WMI?
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2005-11-09

    Table of Contents:
  • Can We Integrate Visual Basic.NET, SQL Server 2000 and WMI?
  • How to connect to SQL Server 2000 using WMI
  • How to retrieve “Databases” information in SQL Server instance
  • How to retrieve “tables” information in SQL Server instance

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Can We Integrate Visual Basic.NET, SQL Server 2000 and WMI? - How to retrieve “Databases” information in SQL Server instance


    (Page 3 of 4 )

    Now we come to dealing with SQL Server information.  How do we retrieve the “databases” available in SQL Server 2000 using WMI?  Actually it is simpler than you might have expected.  Let us go through the following code:

    Private Sub btnListDatabases_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListDatabases.Click
            Dim PrivateLateBoundClass As ManagementClass
            PrivateLateBoundClass = New ManagementClass(Nothing, New ManagementPath("\\server\root\MicrosoftSQLServer:
    MSSQL_Database"), Nothing)
            Dim dbs As ManagementObjectCollection = PrivateLateBoundClass.GetInstances()
            Dim db As ManagementObject
            Dim sdb As String
            For Each db In dbs
                sdb &= db("SQLServerName").ToString & " - " & db("Name").ToString() & ControlChars.NewLine
            Next
            MessageBox.Show(sdb)
        End Sub

    Now, I am working on the “ManagementClass” rather than the “ManagementObject”.  Even though I didn’t specify that it needs to connect to “(local)”, it would automatically get connected to the local database!

    “MSQL_Database” is the second WMI class I used now (in the above code), to retrieve all of the database names existing in the local SQL Server 2000 instance.  As there may exist more than one database, I need to work with the “collection” (which is why I used “ManagementObjectCollection”).  The following is the loop, which helps us to retrieve all the database names automatically.

            Dim dbs As ManagementObjectCollection = PrivateLateBoundClass.GetInstances()
            Dim db As ManagementObject
            Dim sdb As String
            For Each db In dbs
                sdb &= db("SQLServerName").ToString & " - " & db("Name").ToString() & ControlChars.NewLine
            Next

    “GetInstances” now would retrieve all “database” instances.  The loop iterates through each database instance, and appends the value of “SQLServerName” and “Name” (database name), which are WMI properties, to the string “sdb”.  Finally, it displays a message box with the string (list of database names) we framed.

    More Visual Basic.NET Articles
    More By Jagadish Chaterjee


       · Hello guys...I opened the doors of WMI even to the level of SQL Server 2000..Have a...
       · Hi jagdesh, The article explained me more about the use of WMI calls in...
       · Hai Gupta! I am very happy that it helped you. Thanks very much, bye.
     

    VISUAL BASIC.NET ARTICLES

    - Movement and Player Statistics in a VB.NET T...
    - Creating and Drawing a Game Map in VB.NET
    - Working with Classes and Properties for Game...
    - Working with Loops, Arrays, and Collections ...
    - Learning Loops in VB.NET for Game Development
    - Learning VB.NET: Working with Variables, Con...
    - The Basics of VB.NET Through Text Game Devel...
    - Learning VB.NET Through Text Game Development
    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...
    - Polymorphism using Abstract Classes in Visua...
    - Shadowing using Shadows in Visual Basic.NET ...
    - Overloading and Overriding in Visual Basic.N...
    - More on Controlling Windows Fax Services Usi...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway