Free Newsletters:
DatabaseJournal  
DBANews
Database Journal
Search Database Journal:
 
MS SQL Oracle DB2 Access MySQL PostgreSQL PHP SQL Etc SQL Scripts & Samples Links Database Forum DBA Videos
internet.com

» Database Journal Home
» Database News
» DBA Videos
» Database Articles
» Database Tutorials
MS SQL
Oracle
MS Access
MySQL
DB2
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» DBA Jobs
» Sitemap

News Via RSS Feed



follow us on Twitter

Marketplace Partners
Be a Marketplace Partner




internet.commerce
Be a Commerce Partner


















2009: The Year Microsoft 'Gets' Users?

Apple's Jobs: Condition Won't Hinder CEO Duties

LG, Netflix Plan TVs With Streaming Net Video

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Database Journal | DBA Support | SQLCourse | SQLCourse2 | Swynk







Senior Infrastructure Engineer (PA)
Next Step Systems
US-PA-Philadelphia

Justtechjobs.com Post A Job | Post A Resume

Featured Database Articles

MS SQL

October 9, 2000

Utility to Build VB Class to Match SQL Stored Procedures

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()


Go to page: Prev  1  2  3  4  Next  

Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

MS SQL Archives







Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
Using SQLCacheDependency in Ms-SQL 2005 prashant12se 2 January 6th, 12:06 AM
using column name as row value Osho4U 1 December 30th, 08:43 AM
merging 2 rows into 1 row taffer 1 December 30th, 07:38 AM
Help needed on Rollback transaction sukino 3 December 24th, 06:30 PM








internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers