Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

MS SQL

Posted Oct 9, 2000

Utility to Build VB Class to Match SQL Stored Procedures - Page 2

By Danny Lesandrini


Code the Utility

As usual, I am loathe to trudge through what I call the "housekeeping" code. You know what I mean, code to initalize variables and test for nulls. It is necessary and is included in the source code, but I'll refrain from explaining each line of code here.

Instead, I'll focus on the 2 methods that are the most critical:

  • ConnectToDB()
  • CreateCodeFromStoredProcs()


Function: ConnectToDB()

The connection routine references objects from the SQLDMO (Distributed Manangement Objects) and SQLNS (SQL Namespace) libraries. We will be using Server, Database and Namespace objects in the code behind our main form, so the first thing we need to do is declare some form level variables.

    Private oSQLServer As SQLDMO.SQLServer
    Private oCurDB As SQLDMO.Database
    Private objSQLNSObj As SQLNamespaceObject
    Private objSQLNS As SQLNamespace
    Private hArray(10) As Long

Note: The following code was adapted from the Microsoft Sample code that ships with SQL Server. The code in these examples is an excellent source for developers wishing to learn more about how to manipulate the SQL Server DMO and NS objects from code.

If the user has supplied valid connection variables (Server, Database, User ID and Password), then an attempt is made to connect to a database by executing the following sub routine:

' **************** BEGIN CODE HERE ****************
Private Sub ConnectToDB()
    On Error Resume Next

    Dim i As Integer
    Dim strConnect As String
    
    Err.Clear
    Screen.MousePointer = vbHourglass
    
    ' Assume success
    fConnected = True

    ' Set SQLDMO object

    Set oSQLServer = New SQLDMO.SQLServer
    oSQLServer.LoginTimeout = 10
    oSQLServer.ODBCPrefix = False
    
    'Attempt a connection, then fill the properties stuff
    oSQLServer.ApplicationName = "SQL-DMO Explorer"
    oSQLServer.Connect strServer, strUID, strPWD
    oSQLServer.Application.GroupRegistrationServer = ""
    
    ' Set Current SQLDMO Database object
    Set oCurDB = oSQLServer.Databases(strDatabase)
        
    ' Create SQL Namespace Object
    Set objSQLNS = New SQLNamespace
    objSQLNS.Initialize "EMS Admin Namespace", SQLNSRootType_Server, _
                            "Server=" & strServer _
                          & ";UID=" & strUID _
                          & ";pwd=" & strPWD & ";", hWnd
    
    ' get a root object of type Server and walk down the hierarchy from there

    hArray(0) = objSQLNS.GetRootItem
    ' get first level server->databases
    hArray(1) = objSQLNS.GetFirstChildItem(hArray(0), SQLNSOBJECTTYPE_DATABASES)
    ' get second level server->databases->database('pubs')
    hArray(2) = objSQLNS.GetFirstChildItem(hArray(1), SQLNSOBJECTTYPE_DATABASE, strDatabase)
    ' get a SQLNamespaceObject to execute commands against on the wanted level
    Set objSQLNSObj = objSQLNS.GetSQLNamespaceObject(hArray(2))

    Screen.MousePointer = vbDefault

    If Err.Number > 0 Then fConnected = False

End Sub
' ***************** End CODE HERE *****************

If the connection is made successfully, then a flag, fConnected, is set to TRUE. This flag is then used to toggle the form command buttons accordingly so that the user may continue. (See screen shot)




Page 3: Function: CreateCodeFromStoredProcs()




MS SQL Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 




Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
SQL 2005: SSIS: Error using SQL Server credentials poverty 3 August 17th, 07:43 AM
Need help changing table contents nkawtg 1 August 17th, 03:02 AM
SQL Server Memory confifuration bhosalenarayan 2 August 14th, 05:33 AM
SQL Server Primary Key and a Unique Key katty.jonh 2 July 25th, 10:36 AM