![]() |
![]() |
Using the Workspace Manager |
Using Local SAS with ADO |
Dim obSAS As SAS.Workspace Dim obWorkspaceManager As New SASWorkspaceManager.WorkspaceManager Private Sub Form_Load() Dim obConnection As New ADODB.Connection Dim obRecordSet As New ADODB.Recordset Dim errorString As String Set obSAS = obWorkspaceManager.Workspaces.CreateWorkspaceByServer( "MyWorkspaceName", VisibilityProcess, Nothing, "", "", errorString) obSAS.LanguageService.Submit "data a; x=1; y=100; output; x=2; y=200; output; run;" obConnection.Open "provider=sas.iomprovider.1; SAS Workspace ID=" + obSAS.UniqueIdentifier obRecordSet.Open "work.a", obConnection, adOpenStatic, adLockReadOnly, adCmdTableDirect obRecordSet.MoveFirst Debug.Print obRecordSet(1).Value End Sub Private Sub Form_Unload(Cancel As Integer) ' If we don't close SAS, the ' SAS process might run forever If Not (obSAS is Nothing) Then obWorkspaceManager.Workspaces.RemoveWorkspace obSAS obSAS.Close End If End Sub
Using Remote SAS with ADO and No Persisted ServerDefs or LoginDefs |
Dim obSAS As SAS.Workspace Dim obWorkspaceManager As New SASWorkspaceManager.WorkspaceManager Private Sub Form_Load() Dim obConnection As New ADODB.Connection Dim obRecordSet As New ADODB.Recordset Dim obServerDef As New SASWorkspaceManager.ServerDef Dim xmlString As String obServerDef.Port = ObjectServerPort obServerDef.Protocol = ProtocolBridge ' Multiple hostNames can be provided ' for failover; order is unreliable. obServerDef.MachineDNSName = "myServer.myCompany.com" Set obSAS = obWorkspaceManager.Workspaces.CreateWorkspaceByServer( "MyWorkspaceName", VisibilityProcess, obServerDef, "MyUserID", "MyPassword", xmlString) obSAS.LanguageService.Submit "data a; x=1; y=100; output; x=2; y=200; output; run;" obConnection.Open "provider=sas.iomprovider.1; SAS Workspace ID=" + obSAS.UniqueIdentifier obRecordSet.Open "work.a", obConnection, adOpenStatic, adLockReadOnly, adCmdTableDirect obRecordSet.MoveFirst Debug.Print obRecordSet(1).Value End Sub Private Sub Form_Unload(Cancel As Integer) If not (obSAS is Nothing) Then obWorkspaceManager.Workspaces.RemoveWorkspace obSAS obSAS.Close End If End Sub
Parsing the Returned XML |
This example illustrates how to parse the XML that is returned when an error occurs:
Dim obSAS As SAS.Workspace Dim obWorkspaceManager As New SASWorkspaceManager.WorkspaceManager Private Sub Form_Load() Dim errorXML As String On Error GoTo CREATEERROR ' Establish File parameters Set obSAS = obWorkspaceManager.Workspaces.CreateWorkspaceByLogicalName( "MyWorkspaceName", VisibilityProcess, "", "", errorXML) ' errorXML only gets returned when there is a successful connection, ' otherwise err.description gets the XML If (Len(errorXML) > 0) Then ParseXML errorXML CREATEERROR: If (Err.Number <> SASWorkspaceManager.Errors.SEE_XML) Then GoTo TROUBLE End If DISPLAYXML: ParseXML Err.Description GoTo NOTROUBLE TROUBLE: Debug.Print Str(Err.Number) + ": " + Err.Source + ": " + Err.Description NOTROUBLE: End Sub Private Sub ParseXML(xml As String) ' Another option would be to create some XML to make this look nice. ' Write the XML to a file so it can be parsed ' Form2 simply has a WebBrowser control in it Dim f As New Scripting.FileSystemObject Dim fname As String Dim tstream As Scripting.TextStream fname = f.BuildPath(f.GetSpecialFolder(TemporaryFolder), f.GetTempName) Set tstream = f.OpenTextFile(fname, ForWriting, True) tstream.Write ("<html><body><xml id=""combridgeOutput"">") tstream.Write (xml) tstream.Write ("</xml><table datasrc=""#combridgeOutput""><thead> <th>sasServer</th><th>sasLogin</th>") tstream.Write ("<th colspan=50>machineDNSName</th><th colspan=20> port</th><th colspan=40>status</th>") tstream.Write ("<th colspan=200>description</th></thead><tbody><tr> <td><span datafld=""sasserver"">") tstream.Write ("</span></td><td><span datafld=""saslogin""></span></td> <td colspan=50>") tstream.Write ("<span datafld=""sasmachinednsname""></span></td> <td colspan=50><span datafld=""sasport"">") tstream.Write ("</span></td><td colspan=40><span datafld=""status""> </span></td><td colspan=200>") tstream.Write ("<span datafld=""description""></span></td></tr></tbody> </table></body></html>") tstream.Close Form2.WebBrowser1.Navigate "file://" + fname Form2.Show End Sub
Example Code Using COM+ Pooling |
This example illustrates how to use the COM+ pooling feature:
' Either of these lines will create a PooledWorkspace using COM+ dim obPooledWorkspace as new SASWorkspaceManager.PooledWorkspace set obPooledWorkspace = CreateObject("SASWorkspaceManager.PooledWorkspace") ' Note that from this point on, the code is the same in both the COM+ ' and the Integration Technologies pooling. Dim obSAS as SAS.Workspace Set obSAS = obPooledWorkspace.Workspace ' Now use obSAS. Debug.Print obSAS.UniqueIdentifier 'When done, release the obPooledWorkspace to return it to the pool. Set obPooledWorkspace = Nothing
Example Code Using SAS Integration Technologies Pooling |
This example illustrates how to use the SAS Integration Technologies pooling feature:
' Get the pool. If it doesn't exist, create it Dim obWorkspaceManager As New SASWorkspaceManager.WorkspaceManager Dim obPool As SASWorkspaceManager.WorkspacePool On Error Resume Next ' Assume the pool already exists Set obPool = obWorkspaceManager.WorkspacePools("MyLogical") On Error GoTo 0 If (obPool Is Nothing) Then ' The pool doesn't exist; create it Dim obServer As New SASWorkspaceManager.ServerDef Dim obLogin As New SASWorkspaceManager.LoginDef obServer.Protocol = ProtocolBridge obServer.Port = 4321 ' A random number obServer.MachineDNSName = "MyMachine.MyCompany.com" obLogin.LoginName = "MyLogin" obLogin.Password = "MyPassword" Set obPool = obWorkspaceManager.WorkspacePools.CreatePoolByServer( "MyLogical", obServer, obLogin) End If ' Now the pool is created, get a Workspace from it: Dim obPooledWorkspace As SASWorkspaceManager.PooledWorkspace set obPooledWorkspace = obPool.GetPooledWorkspace("", "", 1000) ' Note that from this point on, the code is the same in both the COM+ ' and the Integration Technologies pooling. Dim obSAS As SAS.Workspace Set obSAS = obPooledWorkspace.Workspace ' Now use obSAS. Debug.Print obSAS.UniqueIdentifier 'When done, release the obPooledWorkspace to return it to the pool. Set obPooledWorkspace = Nothing
![]() |
![]() |
Copyright © 2010 by SAS Institute Inc., Cary, NC, USA. All rights reserved.