Using the latest (stable release) of Oracle Developer Tools for Visual Studio 11.1.0.7.20.

Posted by mbcrump on Geeks with Blogs See other posts from Geeks with Blogs or by mbcrump
Published on Sat, 01 May 2010 09:25:29 GMT Indexed on 2010/05/01 16:38 UTC
Read the original article Hit count: 397

Filed under:

image     +image  =

Simple and safe Data connections.

 

This guide is for someone wanting to use the latest ODP.NET quickly without reading the official documentation. This guide will get you up and running in about 15 minutes.

I have reviewed my referral link to my simple Setting up ODP.net with Win7 x64 and noticed most people were searching for one of the following terms:

“how to use odp.net with vs”

“setup connection odp.net”

“query db using odp and vs”

While my article provided links and a sample tnsnames.ora file, it really didn’t tell you how to use it. I’m hoping that this brief tutorial will help.

So before we get started, you will need the following:

It should be noted that The System.Data.OracleClient namespace is the OLD .NET Framework Data Provider for Oracle. It should not be used anymore as it has been depreciated. The latest version which is what we are using is Oracle.DataAccess.Client.

First things first, Add a reference to the Oracle.DataAccess.Client after you install ODP.NET

 

image

Copy and paste the following C# code into your project and replace the relevant info including the query string and you should be able to return data. I have commented several lines of code to assist in understanding what it is doing.

 

Lambda Expression.
  1. using System;
  2. using System.Data;
  3. using Oracle.DataAccess.Client;
  4.  
  5. namespace ConsoleApplication1
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.  
  12.         try
  13.         {
  14.             //Setup DataSource
  15.             string oradb = "Data Source=(DESCRIPTION ="
  16.                                    + "(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = hostname)(PORT = 1521)))"
  17.                                    + "(CONNECT_DATA = (SERVICE_NAME = SERVICENAME))) ;"
  18.                                    + "Persist Security Info=True;User ID=USER;Password=PASSWORD;";
  19.           
  20.             //Open Connection to Oracle - this could be moved outside the try.
  21.             OracleConnection conn = new OracleConnection(oradb);
  22.             conn.Open();
  23.  
  24.             //Create cmd and use parameters to prevent SQL injection attacks.
  25.             OracleCommand cmd = new OracleCommand();
  26.             cmd.Connection = conn;
  27.  
  28.             cmd.CommandText = "select username from table where username = :username";
  29.  
  30.             OracleParameter p1 = new OracleParameter("username", OracleDbType.Varchar2);
  31.             p1.Value = username;
  32.             cmd.Parameters.Add(p1);
  33.  
  34.             cmd.CommandType = CommandType.Text;
  35.  
  36.             OracleDataReader dr = cmd.ExecuteReader();
  37.             dr.Read();
  38.  
  39.             //Contains the value of the datarow
  40.             Console.WriteLine(dr["username"].ToString());
  41.  
  42.             //Disposes of objects.
  43.             dr.Dispose();
  44.             cmd.Dispose();
  45.             conn.Dispose();
  46.         }
  47.  
  48.         catch (OracleException ex) // Catches only Oracle errors
  49.         {
  50.             switch (ex.Number)
  51.             {
  52.                 case 1:
  53.                     Console.WriteLine("Error attempting to insert duplicate data.");
  54.                     break;
  55.                 case 12545:
  56.                     Console.WriteLine("The database is unavailable.");
  57.                     break;
  58.                 default:
  59.                     Console.WriteLine(ex.Message.ToString());
  60.                     break;
  61.             }
  62.         }
  63.  
  64.         catch (Exception ex) // Catches any error not previously caught
  65.         {
  66.     
  67.              Console.WriteLine("Unidentified Error: " + ex.Message.ToString());
  68.              }
  69.         }
  70.  
  71.     }
  72.      
  73.    
  74. }

At this point, you should have a working Program that returns data from an oracle database. If you are still having trouble then drop me a line and I will be happy to assist. As of this writing, oracle has announced the latest beta release of ODP.NET 11.2.0.1.1 Beta.  This release includes .NET Framework 4 and .NET Framework 4 Client Profile support. You may want to hold off on this version for a while as its BETA, and I wouldn’t want any production code using any BETA software.

© Geeks with Blogs or respective owner