Sample Microsoft® Visual Basic® code for using SQL-NS to display the Properties dialog box for the pubs database is located in the \Mssql7\Devtools\Samples\Sqlns\Vb\Dbprop directory. Use these files:
'*********************************************************************
' dbprop - Display Database Properties dialog box using SQL Namespace
'*********************************************************************
Option Explicit
Dim objSQLNS As SQLNamespace
Dim hArray(10) As Long
Private Sub Command1_Click()
On Error GoTo ErrHandler
' 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, "pubs")
' Get a SQLNamespaceObject object to execute commands against at the desired
'level.
Dim objSQLNSObj As SQLNamespaceObject
Set objSQLNSObj = objSQLNS.GetSQLNamespaceObject(hArray(2))
' Execute the command (in this case by name).
objSQLNSObj.Commands("Properties").Execute
Cleanup:
Set objSQLNSObj = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description & " " & Err.Number, vbOKOnly, "Error"
GoTo Cleanup
End Sub
Private Sub Form_Load()
On Error GoTo ErrHandler
' Initialize the SQL Namespace (only once).
Set objSQLNS = New SQLNamespace
objSQLNS.Initialize "SQL Name Space Tester", SQLNSRootType_Server, "Server=.;UID=sa;pwd=;", hWnd
' Get a root object of type Server and walk down the hierarchy from
' there.
hArray(0) = objSQLNS.GetRootItem
Exit Sub
ErrHandler:
MsgBox Err.Description & " " & Err.Number, vbOKOnly, "Error"
' If initializing SQL Namespace did not succeed, do not
' go on. Check if sqlns.dll is registered correctly (using regsvr32
'sqlns.dll). Or maybe you could not connect; the sample program
' looks for a local SQL Server installation.
End
End Sub
Private Sub Form_Terminate()
' Terminate the SQL Namespace
Set objSQLNS = Nothing
End Sub