In this recipe, you learn two ways to assign a libref when you use the IOM Data Provider to open or create a table.
Applies to: | SAS IOM Data Provider |
Implement using: | ADO or OLE DB |
A SAS libref is a label, or an alias, that is temporarily assigned to a folder so that the storage location (the full path, including drive and folder) is in a form that is recognized by SAS. You must use a libref whenever you use the IOM Data Provider to open or create a table. Following are two ways that you can assign a new libref:
Both techniques assign a libref for the duration of the SAS session or until the libref is unassigned. When choosing between the two methods, consider the information in the following table:
If this is true | then it might be best to use |
---|---|
You are comfortable using the SAS LIBNAME statement | the first method |
You are not using the SAS Workspace Manager to connect to your server | the first method, which eliminates the need for the SAS Workspace, Libref and Workspace Manager objects |
You are using the Workspace Manager to connect to your server | the second method (For more information about connecting to an IOM server see Connecting to a Remote IOM Server.) |
You want to know if the LIBNAME statement was successful | the first method, because the AssignLibref method has a return code |
You want to take advantage of other methods and attributes on the Libref object | the second method |
The following Visual Basic code fragment implements these three steps:
Note: For information about how to open a Connection object, see Opening an ADO Connection Object.
Dim rs As ADODB.Recordset Dim cmd As ADODB.Command Set rs = New ADODB.Recordset Set cmd = New ADODB.Command 'Assume that obConnection is an opened ADO Connection object. cmd.ActiveConnection = obConnection 'Execute the Command object. cmd.CommandType = adCmdText 'Assign a libref. cmd.CommandText = "libname mylib 'c:\census\data'" cmd.Execute 'Open a table in that libref. rs.Open "mylib.cities", obConnection, adOpenStatic, adLockReadOnly, adCmdTableDirect
The following Visual Basic code fragment implements these steps:
Dim obSAS As SAS.Workspace Dim obLibRef As SAS.Libref Dim obWM As SASWorkspaceManager.WorkspaceManager Dim obConnection As ADODB.Connection Dim rs As ADODB.Recordset Set obWM = New SASWorkspaceManager.WorkspaceManager Set obConnection = New ADODB.Connection Set rs = New ADODB.Recordset ' Establish a SAS workspace. Set obSAS = obWM.Workspaces.CreateWorkspaceByLogicalName("a", VisibilityProcess, "", "", s) ' Assign a libref. Set obLibRef = obSAS.DataService.AssignLibref("mylib", "", "c:\census\data", "") ' Open a connection. obConnection.Open "Provider=sas.IOMProvider.1;SAS Workspace ID=" & obSAS.UniqueIdentifier ' Open a table in that libref. rs.Open "mylib.cities", obConnection, adOpenStatic, adLockReadOnly, adCmdTableDirect
obSAS, obLibRef and obWM are all SAS Integrated Object Model (IOM) objects. For more information about the SAS object hierarchy and how to instantiate workspace objects independently of the IOM provider, access the documentation for SAS Integration Technologies, which is located at support.sas.com/rnd/itech/library/library9.html. In the Developer's Guide under "Developing Windows Clients," see the section entitled "Class Documentation."