Converting VBA database (Oracle) code to C#

I have the attached working VBA code that retrieves data from an Oracle database and inserts it into an Excel Workbook, in a given column. I need to covert this to C#. And while C# is my primary language, I have never done ADO.NET with Oracle, just Access and SQL Server.

Please provide me the objects I'll need to create the connection, execute the query and retrieve the data.

Is it simply OleDbConnection, OleDbCommand, etc.?


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
            Dim Myconnection As Connection
            Dim Myrecordset As Recordset
            Dim MyWorkbook As String
            'Open database connection
            Set MyDatabase = New ADODB.Connection
            MyDatabase.ConnectionString = "Driver={Oracle in OraClient11g_home1};Data Source=OUR_TEST devsvr;User ID=OUR_TEST;Password=password1;"
            'Open the database
            Set Myrecordset = New Recordset
            'Build a custom query using command object
            Set MyCommand = New ADODB.Command
            With MyCommand
               Set .ActiveConnection = MyDatabase
               .CommandType = adCmdText
            End With
            Myrecordset.Open MyCommand, , adOpenDynamic, adLockReadOnly

            Set Rng = Worksheets("SelectionsDB").Range("A5")
            For Each fld In Myrecordset.Fields
                     Rng.Value = fld.Name
                    Set Rng = Rng.Offset(, 1)
            Next fld
            Worksheets("SelectionsDB").Range("A6").CopyFromRecordset Myrecordset


        End Sub

Open in new window

newbiewebSr. Software EngineerAsked:
Who is Participating?
slightwv (䄆 Netminder)Connect With a Mentor Commented:
>>Is it simply OleDbConnection, OleDbCommand, etc.?

I would strongly suggest you migrate to Oracle's .Net provider, ODP.Net.

There are tons of examples using C# and ODP.Net for you to reference.  The install actually comes with samples.

You can download the ODAC from:

If you have a specific question on how to do something, please ask but this site really isn't set up to port portions of code for people.
newbiewebSr. Software EngineerAuthor Commented:
Thanks. I was really looking for the names of the objects to use in C#. I am glad you mentioned ODP.NET, since that would changes the database ohjects I decide on.

Could you describe one or two big advantages of using ODP.NET over OleDb?


newbiewebSr. Software EngineerAuthor Commented:
slightwv (䄆 Netminder) Commented:
>>one or two big advantages of using ODP.NET over OleDb?

Sorry.  I was out yesterday.

The biggest advantage is:  OleDB is old-school and is pretty much dead.  When using .Net, you should use drivers specifically designed to run with .Net.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.