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
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:
- Download the following: www.oracle.com/technology/software/tech/dotnet/utilsoft.html from oracle and install it. It is the first one on the page.
- Visual Studio 2008 or 2010.
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
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.
- using System;
- using System.Data;
- using Oracle.DataAccess.Client;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- try
- {
- //Setup DataSource
- string oradb = "Data Source=(DESCRIPTION ="
- + "(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = hostname)(PORT = 1521)))"
- + "(CONNECT_DATA = (SERVICE_NAME = SERVICENAME))) ;"
- + "Persist Security Info=True;User ID=USER;Password=PASSWORD;";
- //Open Connection to Oracle - this could be moved outside the try.
- OracleConnection conn = new OracleConnection(oradb);
- conn.Open();
- //Create cmd and use parameters to prevent SQL injection attacks.
- OracleCommand cmd = new OracleCommand();
- cmd.Connection = conn;
- cmd.CommandText = "select username from table where username = :username";
- OracleParameter p1 = new OracleParameter("username", OracleDbType.Varchar2);
- p1.Value = username;
- cmd.Parameters.Add(p1);
- cmd.CommandType = CommandType.Text;
- OracleDataReader dr = cmd.ExecuteReader();
- dr.Read();
- //Contains the value of the datarow
- Console.WriteLine(dr["username"].ToString());
- //Disposes of objects.
- dr.Dispose();
- cmd.Dispose();
- conn.Dispose();
- }
- catch (OracleException ex) // Catches only Oracle errors
- {
- switch (ex.Number)
- {
- case 1:
- Console.WriteLine("Error attempting to insert duplicate data.");
- break;
- case 12545:
- Console.WriteLine("The database is unavailable.");
- break;
- default:
- Console.WriteLine(ex.Message.ToString());
- break;
- }
- }
- catch (Exception ex) // Catches any error not previously caught
- {
- Console.WriteLine("Unidentified Error: " + ex.Message.ToString());
- }
- }
- }
- }
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