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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 6
    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


    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

    - User-defined Functions using Visual Basic Ap...
    - Understanding Object Binding in VBA
    - Mastering the Message Box
    - Testing a Windows Forms Application
    - Using Visual Basic.NET Features to Code a Wi...
    - Correcting Code in a Windows Forms Applicati...
    - Write Readable Code and Comments for Windows...
    - How to Code and Test a Windows Forms Applica...
    - Adding Features to a Windows Forms Applicati...
    - How to Design a Windows Forms Application
    - LINQ to XML Programming Using Visual Basic.N...
    - Understanding Delegates using Visual Basic.N...
    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek