Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Converting VBA database (Oracle) code to C#

Posted on 2011-09-12
4
Medium Priority
?
637 Views
Last Modified: 2012-06-21
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.?


Thanks.

 
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
            MyDatabase.Open
    
            Set Myrecordset = New Recordset
     
            'Build a custom query using command object
            Set MyCommand = New ADODB.Command
            With MyCommand
               Set .ActiveConnection = MyDatabase
               .CommandType = adCmdText
               .CommandText = "SELECT DESCRIPTION FROM thickness WHERE ACTIVE_INDICATOR_LF = 'Y' ORDER BY DESCRIPTION DESC"
            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

            Myrecordset.Close
    
            MyDatabase.Close

        End Sub

Open in new window

0
Comment
Question by:newbieweb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 2000 total points
ID: 36524581
>>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:
http://www.oracle.com/technetwork/developer-tools/visual-studio/downloads/index.html

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.
0
 

Author Comment

by:newbieweb
ID: 36525204
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?


Thanks.

0
 

Author Closing Comment

by:newbieweb
ID: 36525333
Thanks!
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36530394
>>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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question