Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 4 - 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 
Moblin 
JMSL Numerical Library 
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


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


    (Page 4 of 4 )

    Now we come to dealing with SQL Server table information.  How do we retrieve the “tables” available in SQL Server 2000 database using WMI?  Here, in this example, I am trying to filter based on a particular “database”.  Let us go through the following code:

    Private Sub btnListTables_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListTables.Click
            Dim PrivateLateBoundClass As ManagementClass
            PrivateLateBoundClass = New ManagementClass(Nothing, New ManagementPath("\\server\root\MicrosoftSQLServer:
    MSSQL_Table"), Nothing)
            Dim tables As ManagementObjectCollection = PrivateLateBoundClass.GetInstances()
            'loop the information to store in an array
            Dim table As ManagementObject
            Dim sTable As String
            For Each table In tables
                If table("DatabaseName") = "Northwind" Then
                    sTable &= table("SQLServerName").ToString & " - "
                    sTable &= table("DatabaseName").ToString & " - "
                    sTable &= table("Name").ToString() & ControlChars.NewLine
                End If
     
            Next
            MessageBox.Show(sTable)
        End Sub

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

    “MSQL_Table” is the third WMI class I used now (in the above code), to retrieve all of the table names existing in the local SQL Server 2000 database.  As there may exist more than one table in a single database (or more databases), 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 table names of a particular database “Northwind” automatically.

             Dim tables As ManagementObjectCollection =
    PrivateLateBoundClass.GetInstances()

            'loop the information to store in an array
            Dim table As ManagementObject
            Dim sTable As String
            For Each table In tables
                If table("DatabaseName") = "Northwind" Then
                    sTable &= table("SQLServerName").ToString & " - "
                    sTable &= table("DatabaseName").ToString & " - "
                    sTable &= table("Name").ToString() & ControlChars.NewLine
                End If
     
            Next

    “GetInstances” now would retrieve all “tables” available.  The loop iterates through each table or WMI instance, and appends the value of “SQLServerName” , “DatabaseName” and “Name” (table name), which are WMI properties, to the string “sTable”.  Finally, it displays a message box with the string (list of table names) we framed.  You can also observe that I filtered the tables of a particular database using an “IF” condition.

    Summary:

    I hope now you can confirm that we can use WMI to interact with SQL Server 2000 together with the .NET framework.  As this is an introductory article, I could not go up to the level of “managing SQL Server 2000”.  But you can expect a few more articles on this subject soon.  Check regularly on this site or sign up for the newsletter.

    You can further enhance the coding I provided with list boxes, dropdown lists, and so forth.  You can also shift this code to ASP.NET.  And that gives us a way to manage SQL Server 2000 using a web application with WMI!

    Any comments, suggestions, bugs, errors, feedback etc. are highly appreciated at jag_chat@yahoo.com.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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

    - 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...
    - Creating and Drawing a Game Map in VB.NET (F...
    - 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 ...





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