MS SQL Server
  Home arrow MS SQL Server arrow Retrieving SQL Server 2005 Database Info u...
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 
VeriSign Whitepapers 
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? 
MS SQL SERVER

Retrieving SQL Server 2005 Database Info using SMO: Scripting Tables, Views, Stored Procedures
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 14
    2006-02-14

    Table of Contents:
  • Retrieving SQL Server 2005 Database Info using SMO: Scripting Tables, Views, Stored Procedures
  • Populating the dropdown lists for “databases” and “tables” using SMO
  • How to retrieve all column info from an SQL Server instance using SMO
  • How to script an SQL Server table using SMO
  • Adding a few more routines to util.vb
  • How to script stored procedures and views (or others) in SQL Server using SMO

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Retrieving SQL Server 2005 Database Info using SMO: Scripting Tables, Views, Stored Procedures


    (Page 1 of 6 )

    This article is the third in a series focusing on retrieving SQL Server 2005 information using SMO along with Visual Basic 2005 and Visual Studio 2005. In this article, we mainly deal with four topics: retrieving the column information of a selected table; scripting the selected “table;” scripting the selected “stored procedure;” and scripting the selected “view.”
    A downloadable file for this article is available here.

    Since I already explained SMO, DMO and more in the first article of this series, I shall dive directly into the technical issues of using SMO.  I also covered the concept of “working with SMO using Visual Studio 2005” in my first article.  If you are new to SMO (or having any trouble working with a new Visual Studio 2005 solution), I strongly suggest you go through that article.

    Creating our own flexible routines to make them reusable

    The following function “getStructColumnBasic” is one of the routines which is defined in “util.vb” (from the downloadable zip) to mainly hold database column information. 

    Public Function getStructColumnBasic() As DataTable
            Dim dt As New DataTable
            With dt.Columns
                .Add(New DataColumn("ColumnName"))
                .Add(New DataColumn("DataType"))
                .Add(New DataColumn("IsIdentity"))
                .Add(New DataColumn("IsPrimaryKey"))
                .Add(New DataColumn("IsForeignKey"))
                .Add(New DataColumn("CanContainNulls"))
                .Add(New DataColumn("Indexes"))
                .Add(New DataColumn("Relations"))
            End With
            Return dt
        End Function

    It mainly creates a data table with a column structure to hold “database column” information from a particular table in a database existing in an SQL Server instance. The column “ColumnName” holds the column name. The column “DataType” holds the data type of the column, and the naming scheme works similarly for the rest.

    The following is another routine which returns a “list of databases” (defined in “util.vb”):

    Public Function getDatabaseList(ByRef svr As Microsoft.SqlServer.Management.Smo.Server) As DataTable
            Dim dt As New DataTable
            With dt.Columns
                .Add("DatabaseName")
            End With
     
            For Each db As Microsoft.SqlServer.Management.Smo.Database In svr.Databases
                Dim dr As DataRow = dt.NewRow
                dr("DatabaseName") = db.Name
                dt.Rows.Add(dr)
            Next
            Return dt
        End Function

    The following is another routine which returns a “list of tables” (defined in “util.vb”):

    Public Function getTableList(ByRef svr As Microsoft.SqlServer.Management.Smo.Server, ByRef db As Microsoft.SqlServer.Management.Smo.Database) As DataTable
            Dim dt As New DataTable
            With dt.Columns
                .Add("TableName")
                .Add("Schema")
            End With
     
            For Each tbl As Microsoft.SqlServer.Management.Smo.Table In db.Tables
                Dim dr As DataRow = dt.NewRow
                dr("TableName") = tbl.Name
                dr("Schema") = tbl.Schema
                dt.Rows.Add(dr)
            Next
     
            Return dt
        End Function

    More MS SQL Server Articles
    More By Jagadish Chaterjee


       · Hai everybody. This is another interesting article on SQL Server 2005 SMO, enjoy. ...
     

    MS SQL SERVER ARTICLES

    - Completing the Introduction to Transact-SQL
    - A Brief Introduction to Transact-SQL
    - Lookups and Blocking Bad Data
    - Field Validation Rules for Blocking Bad Data
    - Using Masks to Block Bad Data
    - Blocking Bad Data
    - Using @@ROWCOUNT and TABLE Variables for Dat...
    - How to Use Variables, IF and CASE in Databas...
    - Creating Important Aspects of Notification S...
    - Working wth Variables in Database Interactio...
    - Delving Deeper into Notification Services
    - Notification Services
    - Building a Multi-table Report with SQL 2005 ...
    - A Secure Way of Building Connection Strings
    - Transferring a Database Using the SSIS Desig...





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