This sample demonstrates how you can use C# and VB.NET with both ActiveX Data Objects (ADO) and ADO.NET to retrieve data from a SAS Integrated Object Model (IOM) Server using the SAS IOM Object Linking and Embedding Database (OLE DB) Data Provider. Each source file included in this sample can be compiled into an executable program. Each program takes one command line argument: a data set name in the form <libname>
.<memname>
. The programs then try to connect to a local instance of a SAS IOM Server and read the given data set with the SAS IOM OLE DB Data Provider.
Specifically, this sample demonstrates how to do the following in both C# and VB.NET:
Note: Update access is not supported with the SAS OLE DB Providers or with ADO.NET. If you want update access, you must use ADO.
For more information about the SAS OLE DB Providers, see the "SAS Providers for OLE DB" documentation.
The following sections describe the components of this sample:
First, these programs must access the <libname>
.<memname>
from the command line. The code to perform this task is the same for both ADO and ADO.NET. The GetCommandLineArgs method on the Environment object returns an array of strings, the first of which is the name of the program that is invoked. The second element in the array should be the <libname>
.<memname>
to be read.
// Get the command line arguments String[] args = Environment.GetCommandLineArgs(); // The first element will be the name of the running program // so there should be two arguments in the list. if( args.Length < 2 ) { Console.WriteLine( "Required arguments: <libname>.<tablename>." ); Environment.Exit( -1 ); } String itable = args[1];
' Get the command line arguments Dim args As String() = Environment.GetCommandLineArgs() ' The first element will be the name of the running program ' so there should be two arguments in the list. If args.Length < 2 Then Console.WriteLine( "Required arguments: <libname>.<tablename>." ) Environment.Exit( -1 ) End If Dim itable As String = args(1)
Next, a connection must be made to the SAS IOM Server.
In ADO, the ADO Connection class is used to connect to the IOM Server. First, a new Connection object is created and then the Open method is called with a connection string that identifies the provider (SAS IOM OLE DB Data Provider in this case) as well as the server (a local server in this case) and any other properties necessary for the connection. Other providers and servers can be chosen by changing this connection string.
// Create a new ADO Connection object ADODB.Connection adoConnection = new ADODB.Connection(); try { // Open the connection and print the version number. // Other providers can be used by changing the ConnectionString. adoConnection.Open("Provider=sas.IOMProvider.1; Data Source=_LOCAL_;", "", "", 0); Console.WriteLine( "SAS Server Version: " + adoConnection.Properties["DBMS Version"].Value );
' Create a new ADO Connection object Dim adoConnection as ADODB.Connection = new ADODB.Connection() Try ' Open the connection and print the version number. ' Other providers can be used by changing the ConnectionString. adoConnection.Open("Provider=sas.IOMProvider.1; Data Source=_LOCAL_;", "", "", 0) Console.WriteLine( "SAS Server Version: " & _ adoConnection.Properties("DBMS Version").Value )
Using the ADO.NET OleDbConnection class is similar to using the ADOConnection class. First, a new OleDbConnection object is created. The ConnectionString property is then set to the same connection string used with ADO and the Open method is called. Again, other providers and servers can be chosen by changing the connection string.
// Create a new OleDbConnection and tell it to use the IOM provider. // Other providers can be used by changing the ConnectionString. OleDbConnection cn = new OleDbConnection(); cn.ConnectionString = "Provider=sas.IOMProvider; Data Source=_LOCAL_"; try { // Open the connection and print the version number. cn.Open(); Console.WriteLine( "SAS Server Version: " + cn.ServerVersion );
' Create a new OleDbConnection and tell it to use the IOM provider. ' Other providers can be used by changing the ConnectionString. Dim cn As OleDbConnection = New OleDbConnection() cn.ConnectionString = "Provider=sas.IOMProvider; Data Source=_LOCAL_" Try ' Open the connection and print the version number. cn.Open() Console.WriteLine( "SAS Server Version: " & cn.ServerVersion )
Then, the data set specified on the command line must be opened.
In ADO, opening a data set is accomplished by creating an ADO Recordset object and calling its Open method with the name of the data set.
// Open a Recordset on the specified dataset. ADODB.Recordset adoRecordset = new ADODB.Recordset(); adoRecordset.ActiveConnection = adoConnection; // Perform the open adoRecordset.Open(itable, Missing.Value, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly, (int)ADODB.CommandTypeEnum.adCmdTableDirect );
' Open a Recordset on the specified dataset. Dim adoRecordset as ADODB.Recordset = new ADODB.Recordset() adoRecordset.ActiveConnection = adoConnection ' Perform the open adoRecordset.Open(itable, Missing.Value, ADODB.CursorTypeEnum.adOpenForwardOnly, _ ADODB.LockTypeEnum.adLockReadOnly, ADODB.CommandTypeEnum.adCmdTableDirect )
Opening a data set in ADO.NET can be accomplished by creating an OleDbCommand object, setting its CommandType and CommandText properties and then calling ExecuteReader to get an OleDbDataReader object.
// Create an OleDbCommand object and tell it // to open the specified table. OleDbCommand cmd = cn.CreateCommand(); cmd.CommandType = CommandType.TableDirect; cmd.CommandText = itable; // Execute the command and get an OleDbDataReader object. OleDbDataReader reader = cmd.ExecuteReader();
' Create an OleDbCommand object and tell it ' to open the specified table Dim cmd As OleDbCommand = cn.CreateCommand() cmd.CommandType = CommandType.TableDirect cmd.CommandText = itable ' Execute the command and get an OleDbDataReader object. Dim reader As OleDbDataReader = cmd.ExecuteReader()
The next step is to print a list of the column names.
In ADO, each column is represented by a Field object in the Fields collection of the Recordset. The Name property of a Field object contains the name of that column.
// Print the column names. Console.Write( "Columns: " ); for( int i = 0; i < adoRecordset.Fields.Count; ++i ) { if( i > 0 ){ Console.Write( ", " ); } Console.Write( adoRecordset.Fields[i].Name ); } Console.WriteLine();
' Print the column names. Console.Write( "Columns: " ) Dim i as Integer For i = 1 To adoRecordset.Fields.Count If i > 1 Then Console.Write( ", " ) Console.Write( adoRecordset.Fields(i-1).Name ) Next Console.WriteLine()
The GetSchemaTable method on the OleDbDataReader class provides access to the column names in ADO.NET. Each row in the returned DataTable represents a column in the data set. The "ColumnName" column in the schema DataTable contains the name of the column. For each row in the schema DataTable we, therefore, print out the value of the "ColumnName" column.
// Get the Schema information so we can print out the column names. DataTable schema = reader.GetSchemaTable(); Console.Write( "Columns: " ); for( int i = 0; i < schema.Rows.Count; ++i ) { if( i > 0 ){ Console.Write( ", " ); } Console.Write( schema.Rows[i]["ColumnName"] ); } Console.WriteLine();
' Get the Schema information so we can print out the column names. Dim schema As DataTable = reader.GetSchemaTable() Console.Write( "Columns: " ) Dim i as Integer For i = 1 To schema.Rows.Count If i > 1 Then Console.Write( ", " ) Console.Write( schema.Rows(i-1).Item("ColumnName") ) Next Console.WriteLine()
Then, the values for the rows themselves are printed.
In ADO, the same Field object that contained the column's name also contains that column's value in the current row. When a Recordset is first opened, it is positioned on the first row. Our code must iterate over each field, printing its Value property, and then call the MoveNext method on the Recordset to advance to the next row. Once we have visited each Field in each row, the Recordset is closed.
// Print out the values in each row. Console.WriteLine( "Values: " ); int nRows = 0; while( !adoRecordset.EOF ) { for( int j = 0; j < adoRecordset.Fields.Count; ++j ) { if( j > 0 ){ Console.Write( ", " ); } // If a missing value was found print "-missing-" // otherwise convert the value to a string and print it. Type t = adoRecordset.Fields[j].Value.GetType(); if( Type.GetTypeCode( t ) == TypeCode.DBNull ) { Console.Write( "-missing-" ); } else { Console.Write( adoRecordset.Fields[j].Value.ToString() ); } } Console.WriteLine(); adoRecordset.MoveNext(); nRows += 1; } Console.WriteLine( "Read " + nRows + " rows." ); adoRecordset.Close();
' Print out the values in each row. Console.WriteLine( "Values: " ) Dim nRows as Integer nRows = 0 While Not adoRecordset.EOF For i = 1 To adoRecordset.Fields.Count If i > 1 Then Console.Write( ", " ) ' If a missing value was found print "-missing-" ' otherwise convert the value to a string and print it. Dim t as Type t = adoRecordset.Fields(i-1).Value.GetType() If Type.GetTypeCode( t ) = TypeCode.DBNull Then Console.Write( "-missing-" ) Else Console.Write( adoRecordset.Fields(i-1).Value.ToString() ) End If Next Console.WriteLine() adoRecordset.MoveNext() nRows += 1 End While Console.WriteLine( "Read " & nRows & " rows." ) adoRecordset.Close()
In ADO.NET, the OleDbDataReader we obtained from the ExecuteReader method is used to get the actual data. The read method is called to advance the OleDbDataReader to the next row and the GetValue method returns the value for a particular column in the current row. In contrast to the ADO Recordset, the OleDbDataReader is not positioned on the first row after the Open. The first read call positions to the first row. The OleDbDataReader is closed after all the data has been printed.
// Print out the values in each row. Console.WriteLine( "Values: " ); int nRows = 0; while( reader.Read() ) { for( int j = 0; j < reader.FieldCount; ++j ) { if( j > 0 ){ Console.Write( ", " ); } // If the value is null then print "-missing-" // otherwise convert the value to a string and print it. if( reader.IsDBNull( j ) ) { Console.Write( "-missing-" ); } else { Console.Write( reader.GetValue( j ).ToString() ); } } Console.Write( "\n" ); nRows += 1; } Console.WriteLine( "Read " + nRows + " rows." ); reader.Close();
' Print out the values in each row. Console.WriteLine( "Values: " ) Dim nRows as Integer nRows = 0 While reader.Read() For i = 1 To reader.FieldCount If i > 1 Then Console.Write( ", " ) ' If the value is missing then print "-missing-" ' otherwise convert the value to a string and print it. If reader.IsDBNull(i-1) Then Console.Write( "-missing-" ) Else Console.Write( reader.GetValue(i-1).ToString() ) End If Next Console.WriteLine() nRows += 1 End While Console.WriteLine( "Read " & nRows & " rows." ) reader.Close()
The last step is to close the connection to the server and exit the program.
} catch (Exception e) { Console.WriteLine( "Exception: " + e.ToString() ); } if ( ( adoConnection != null ) && ( adoConnection.State != (int)ADODB.ObjectStateEnum.adStateClosed ) ) { adoConnection.Close(); } adoConnection = null;
Catch e As Exception Console.WriteLine( "Exception: " & e.ToString() ) End Try If ( Not adoConnection Is Nothing ) AndAlso _ ( adoConnection.State <> ADODB.ObjectStateEnum.adStateClosed ) Then _ adoConnection.Close() adoConnection = Nothing
} catch( Exception e ) { Console.WriteLine( "Exception: " + e.ToString() ); } if ( ( cn != null) && ( cn.State != ConnectionState.Closed ) ) { cn.Close(); } cn = null;
Catch e As Exception Console.WriteLine( "Exception: " & e.ToString() ) End Try If ( Not cn Is Nothing ) AndAlso _ ( cn.State <> ConnectionState.Closed ) Then _ cn.Close() cn = Nothing
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
This sample includes the following source files:
To run this sample, you must have the following software installed on your computer:
To install this sample:
The package contains the following files:
tlbimp msado15.dll
Note: You might have to specify the full path to msado15.dll. For example:
tlbimp C:\Program Files\Common Files\System\ado\msado15.dll
csc /t:exe /r:System.dll,System.Data.dll,ADODB.dll ADOReadCS.cs
vbc /t:exe /r:System.dll,System.Data.dll,ADODB.dll ADOReadVB.vb
csc /t:exe /r:System.dll,System.Data.dll ADO.NETReadCS.cs
vbc /t:exe /r:System.dll,System.Data.dll ADO.NETReadVB.vb
ADOReadCS <libname>.<memname>
ADOReadVB <libname>.<memname>
ADO.NETReadCS <libname>.<memname>
ADO.NETReadVB <libname>.<memname>
Note: Replace <libname>
.<memname>
with the libname and memname of a data set accessible by a local instance of an IOM Server. Data sets in the Sasuser and Sashelp libraries are good candidates.
Type: | Sample |
Topic: | Third Party ==> Programming ==> .NET |
Date Modified: | 2005-06-11 03:00:11 |
Date Created: | 2005-04-01 10:37:27 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | SAS Integration Technologies | Windows | n/a | n/a |