Friday, February 12, 2010

Reading a SAS Dataset with .NET OLEDB

If you have had to read in a SAS Dataset into a .NET Dataset here is some code I found that works to do it with:


Make sure you add these lines to the start of the page

Imports SAS
Imports SASWorkspaceManager
Imports System.Text
Imports System.Data.OleDb


In the Subroutine do this:
 (note: BoldIalics means you should type in your information not what I wrote)
  
   Try  'NEVER FORGET TRY CATCH!!

            Dim sasDs As System.Data.DataSet = New System.Data.DataSet
            Dim sas As OleDbConnection = New OleDbConnection("Provider=sas.LocalProvider; Data Source=pathToDirectoryWhereSASFILEisLocated")
            sas.Open()
            Dim sasCommand As OleDbCommand = sas.CreateCommand
            sasCommand.CommandType = CommandType.TableDirect
            sasCommand.CommandText = "nameOfSASFilewithoutExtension"
            Dim da As OleDbDataAdapter = New OleDbDataAdapter(sasCommand)
            da.Fill(sasDs, "SasData")
            sas.Close()
            DataGridView1.DataSource = sasDs.Tables("SasData")
        Catch ex As Exception
            MessageBox.Show("Unable to load SAS dataset. Error seen was: " + ex.ToString)
        End Try

    End Sub

No comments:

Post a Comment