The main SQL-NS interface is the SQLNamespace object, which enumerates the objects in the namespace. Individual objects are identified by the HSQLNSITEM handle. A SQLNamespaceObject interface can be obtained for this handle by calling the GetSQLNamespaceObject method.
The steps to invoke a user interface component are:
Dim oSQLNS As SQLNamespace
Set oSQLNS = New SQLNamespace
' or using late binding
Dim oSQLNS As Object
Set oSQLNS = CreateObject("SQLNS.SQLNamespace")
ISQLNamespace * pNS = NULL;
CoInitialize(NULL);
CoCreateInstance(CLSID_SQLNamespace, NULL, LSCTX_INPROC_SERVER, IID_ISQLNamespace, (LPVOID*)&pNS);
The first two constants, SQLNSRootType_DefaultRoot and SQLNSRootType_ServerGroup, retrieve their connection information from the registration information of SQL Server Enterprise Manager. Because this depends on a user’s individual configuration, it is not recommended that an application use these two SQLNSRootType constants as a starting point. Instead, an application should use SQLNSRootType_Server or SQLNSRootType_Database because they give you complete control over the destination without having to depend on an individual user’s SQL Server Enterprise Manager configuration.
If ROOTTYPE is SQLNSRootType_DefaultRoot, no connection string is required because all necessary information is retrieved from the SQL Server Enterprise Manager registration information on the local computer. If ROOTTYPE is SQLNSRootType_ServerGroup, the connection string only needs to contain the SvrGrp=SQL Server Group entry, because all other information is retrieved from SQL Server Enterprise Manager registry settings on the local computer. If SQLNSRootType_Server is used, an application must specify a complete connection string because no SQL Server Enterprise Manager registration information is available. The complete initialization and the connection string should look like this:
' Initialize root object
eSQLNSRootType = SQLNSRootType_Server
strConnectString = String(255, 0)
strConnectString = "Server=MYSERVER;Trusted_Connection=Yes;"
strAppName = "SQLNS Application; "
objSQLNS.Initialize strAppName, eSQLNSRootType, Str(strConnectString), hWnd
If objSQLNS Is Nothing Then
MsgBox "SQLNamespace could not be initiated. Terminating.", vbOKOnly, "Error"
End If
V_BSTR(&var) = SysAllocString(L"Server=.;Trusted_Connection=Yes;");
pNS->Initialize(L"SQLNSX Test App", SQLNSRootType_Server, &var, NULL);
‘If the ROOTTYPE is SQLNSRootType_Database, add
‘Database=pubs;
‘to the connectstring. Otherwise, the connectstring is the same.
Dim hRootItem As Long
hRootItem = objSQLNS.GetRootItem
All HSQLNSITEM types are stored as Long in Microsoft® Visual Basic®. When a HSQLNSITEM is zero after an assignment, an error occurred. A good practice is to check for a zero HSQLNSITEM handle in your debug build by using asserts like this:
Debug.Assert hRootItem = 0
To test whether the object type of hRootItem is the same as the root type passed in to call to Initialize, an application can call objSQLNS.GetType(hRootItem) which returns the object type.
HSQLNSITEM hServer;
pNS->GetRootItem(&hServer);
assert (hServer);
Dim hRootItem As Long
Dim hDatabases As Long
Dim hDatabase As Long
Dim hUsers As Long
Dim hUser As Long
hRootItem = objSQLNS.GetRootItem
hDatabases = objSQLNS.GetFirstChildItem(hRootItem, _ SQLNSOBJECTTYPE_DATABASES)
hDatabase = objSQLNS.GetFirstChildItem(hDatabases, _SQLNSOBJECTTYPE_DATABASE, "master")
hUsers = objSQLNS.GetFirstChildItem(hDatabase, _SQLNSOBJECTTYPE_DATABASE_USERS)
hUser = objSQLNS.GetFirstChildItem(hUsers, _SQLNSOBJECTTYPE_DATABASE_USER, "dbo")
HSQLNSITEM hServer, hDBs, hDB, hUsers, hUser;
pNS->GetRootItem(&hServer);
pNS->GetFirstChildItem(hServer, SQLNSOBJECTTYPE_DATABASES, NULL, &hDBs);
pNS->GetFirstChildItem(hDBs, SQLNSOBJECTTYPE_DATABASE, L"pubs", &hDB);
pNS->GetFirstChildItem(hDB, SQLNSOBJECTTYPE_DATABASE_USERS, NULL, &hUsers);
pNS->GetFirstChildItem(hUsers, SQLNSOBJECTTYPE_DATABASE_USER, L"dbo", &hUser);
Dim objSQLNSObj As SQLNS.SQLNamespaceObject
Set objSQLNSObj = objSQLNS.GetSQLNamespaceObject(hUser)
ISQLNamespaceObject* pObjUser = NULL;
pNS->GetSQLNamespaceObject(hUser, &pObjUser);
You can enumerate all available commands on SQLNamespaceObject objects by iterating through the Commands collection.
objSQLNSObj.ExecuteCommandByName "Properties", hWnd, SQLNamespace_PreferModal
The other two parameters are the handle to the parent window, and the window modality. The modality can be one of three values that indicate the preferred modality:
Note The SQL Namespace can overrule the requested modality due to default characteristics of the dialog box invoked.
The preferred way to execute a command by ID is to passing in a constant that indentifies the command:
objSQLNSObj.ExecuteCommandByID SQLNS_CmdID_PROPERTIES, hWnd, SQLNamespace_PreferModal
An application can also iterate through the Commands collection on the SQLNamespaceObject object. Each item of the collection represents a SQLNamespaceCommand, which has a Execute method.