Connecting to a Remote SAS Workspace Server Using SAS Objects

Goal

You want your application to connect to a remote SAS Workspace Server by using SAS objects. This connection method provides more flexibility in creating and managing SAS workspaces than using ADO to create the connection.
This recipe applies to the IOM provider. This recipe applies only to ADO. Sample code is included.
Note: To use the sample code, you must reference these type libraries in your Visual Basic project: Microsoft ActiveX Data Objects Library, the SAS Integrated Object Model (IOM) Type Library, and the SASObjectManager Type Library. The SAS type libraries are installed with SAS Integration Technologies.

Implementation

Sample Code That Uses SAS Objects to Connect to a Remote SAS Workspace Server

The following Visual Basic code works with all versions of SAS. In these examples, the SAS Workspace Server name is workspace.example.com and the port number is 8591.
    Dim obConnection As New ADODB.Connection
    Dim obSAS As SAS.Workspace
    Dim obOF As New SASObjectManager.ObjectFactory
    Dim obOK As New SASObjectManager.ObjectKeeper         
    Dim obServerDef As New SASObjectManager.ServerDef
           
    ' Use ServerDef attributes to identify the remote server.
    obServerDef.Protocol = ProtocolBridge
    obServerDef.MachineDNSName = "workspace.example.com"
    obServerDef.Port = 8591

   ' Create a workspace on the identified remote server.
    Set obSAS = obOF.CreateObjectByServer("MyServer", True, obServerDef, "johnd", "xyz")

   ' Add the new workspace object to the ObjectKeeper.
    Call obOK.AddObject(1, "WorkspaceObject", obSAS)

   ' Open a connection to the workspace by using its UniqueIdentifier, which is generated automatically.
    obConnection.Open "Provider=sas.iomprovider.9.2; Data Source=iom-id://" & obSAS.UniqueIdentifier
      :
      :
      :
    obConnection.Close
   
   ' Remove the workspace object from the ObjectKeeper.
    Call obOK.RemoveObject(obSAS)

A Closer Look at the SAS Objects

Here is more information about the SAS objects that are used in the sample code:
  • The ServerDef object specifies the set of attributes necessary to make a connection to the remote SAS Workspace Server. The sample code assigns values to these attributes: Protocol, MachineDNSName, and Port.
  • The following line of code uses the ObjectFactory CreateObjectByServer method in order to create a new SAS workspace on the identified server.
    Set obSAS = obOF.CreateObjectByServer("MyServer", True, obServerDef, "johnd", "xyz")
    The first parameter is a name that you assign to the created server instance. The second parameter is the Boolean value True, which indicates that the application should not attempt to create the workspace until a connection has been established. The third parameter is the name of the ServerDef object that contains the connection information. The fourth and fifth parameters are the user ID and password that are required to log on to the remote server.
  • The following line of code uses the ObjectKeeper AddObject method in order to store the new SAS workspace. Before the IOM provider can reference the workspace, the workspace must be added to the ObjectKeeper.
    Call obOK.AddObject(1, "WorkspaceObject", obSAS)
    The first two parameters are a unique numeric ID and a unique string. Either value can be used in later code in order to retrieve the workspace object. The third parameter is the name of the workspace object.
Note: For information about working with object variables and creating a workspace, see the SAS Integration Technologies: Windows Client Developer's Guide.