Mathew Nolton Blog

Software dementia unleashed...

Oracle OleDb Provider for .Net and Stored Procedures and Ref Cursors

For those of you working with Oracle and using the Oracle OleDb Provider for .Net. In other words, your connectionstring provider looks like Provider=OraOLEDB.Oracle;....... as opposed to Provider=MSDAORA;......

You probably have come across the issue of wanting to get a ref cursor back from a stored procedure. If you haven't already noticed, it doesn't work without some modifications to your connection string:

If your procedure has the following signature:

PROCEDURE someProcedure(identifier in varchar,out_cursor out CURSOR_DEFINITION);

Then your connectionstring should look like this:

Provider=OraOLEDB.Oracle;OLEDB.NET=true;PLSQLRSet=true;Password=[password];User ID=[userid];Data Source=[DataSource]

Notice the following differences about this connectionstring

  • PLSQLRSet=true   This states that resultsets can be returned from stored procedures.
  • OLEDBNet=True   This means that OraOLEDB is compatible with the OLE DB .Net data provider. If you set this to true, it cannot be used with ADO.

Unfortunately, you cannot set these attributes at the object level like you could with ADO.

Your c# code, would look something like this:

using( OleDbConnection oracleConn = new OleDbConnection( _connectionString ) )
{
   // open connection
   try{oracleConn.Open();}
   catch{throw new LogonException();}
   // build command
   OleDbCommand cmd =
new OleDbCommand("{call someProcedure(?)}", oracleConn );
   cmd.CommandType = CommandType.Text;
   // add parameters
   cmd.Parameters.Add( "identifier", OleDbType.VarChar ).Value = identifier;
   OleDbDataAdapter da =
new OleDbDataAdapter( cmd );
   // fill dataset
   DataSet ds = new DataSet();
   da.Fill( ds);
   // all done, return
   return
ds;
}
 
-Mathew Nolton
Posted: Nov 20 2003, 01:04 PM by MatiasN | with 12 comment(s)
Filed under:

Comments

Jason Tucker said:

I ran into stuff like this at the beginning of the year [http://weblogs.asp.net/jtucker/posts/2965.aspx]. I opted for using Oracle's provider even in it's infancy since it was faster then MS's and a boat load faster then OleDB. RefCursors are easily returned as regular datasets without modification ot your connection string.
# November 20, 2003 1:57 PM

Mathew Nolton said:

I agree. At a previous consultation we used the OracleProvider as opposed to Microsoft's and the OleDb provider. It was faster, but it had some other issues (it didn't always release cursors). However, at times you don't always have control over what provider you can use.
-Mathew Nolton
# November 20, 2003 2:26 PM

Jason Tucker said:

I feel your pain, luckily we got control over the tech that was used for the site we did. I've also started testing the CoreLabs Oracle provider (http://www.crlabs.com). They say they can handle all Ora9i datatypes, which neither provider can do now.
# November 20, 2003 2:28 PM

hugo158@hotmail.com said:

can i use this sample in the same way to sql server and oracle.
i mean, with oledb i can make an app web that it use so sql server or oracle in the same way.
thanks
# January 5, 2004 4:06 PM

Mathew Nolton said:

Hugo,
You can use the same "basic" code but you will need to differentiate the connection strings. However, you will find that the SqlClient is MUCH faster for connecting to SqlServer then OleDb is. Also, the Oracle Provider is a bit faster then OleDb for Oracle. The advantage of course is using the same basic code for different backends (e.g. not withstanding the connectionstring).

-Mathew Nolton
# January 5, 2004 6:03 PM

Oliver Castillo said:

Me salvó la vida !!!!!!!!!!!!!!
# January 17, 2004 12:02 AM

Prabhakar Chol said:

Can you please show an example with a procedure an VB.NET code?

Thanks
# February 18, 2004 5:01 PM

Mathew Nolton said:

prabhakar
The change to do this is really in the connection string and not the code per se.

also, i am not a vb.net person and anything i write without a compiler in front of me is bound to be wrong; however, try googling and get some code, then use the connection string described in this blog.

HTH,
-Mathew Nolton

# February 21, 2004 6:35 PM

Andrew Todd said:

Mathew & Co,

I'm trying to run an intranet site setup on the same machine as the Oracle server and absolutely cannot correctly create a connection to Oracle 8x.

I'm using .NET 2003 and no matter what type of connection I try and create (OLE, ODBC or Oracle) I get one or another error messages.

Do you have to have an Oracle CLIENT installed on the host machine (as well as the SERVER) in order to connect to it? I get an error with a reference to a missing OCI.dll.

Otherwise can ANYONE give me an example of a valid connection string for either of the following objects :

- OLEDBConnection
- OracleConnection
- ODBCConnection


Any help would be very much appreciated.

Cheers
Andrew Todd (Oracle Novice)
# February 25, 2004 11:32 PM

Mathew Nolton said:

yes. you need to install the oracle client on your machine. you should use the oracle client that oracle recommends with your particular version.

Per connection strings, you can get all of this from msdn but i also found a neat little site that's easy to remember and is usually correct. http://www.connectionstrings.com/

I can tell you that i am using the oracleclient and the following works for me:

Password=userid;User ID=password;Data Source=datasource

and using oledb...the following worked
Provider=OraOLEDB.Oracle;PLSQLRSet=true;Password=password;User ID=userid;Data Source=datasource

note with oledb i have plsqlrset=true this is to handle refcursors as results sets using oledb...something you must add. with the old activex version of the provider you could set this progamatically. with the .net provider you must set it in the connection string.

HTH,
Mathew Nolton



# February 26, 2004 8:29 AM

Tim Cavanaugh said:

Andrew,

If you are trying to connect to Oracle via an ASP.NET you also need to setup the NTFS permissions for the machine's ASPNET account (this is the ASP.NET worker process).

The machine running the IIS server needs to have the Oracle client installed, and the ASPNET account needs the following permissions to the \oracle\ora92\bin folder:

Read & Execute
List Folder Contents
Read

HTH,
Tim Cavanaugh
# February 26, 2004 2:22 PM

raja k said:

executing oracle sp through oracle connection

namespace : system.data.oracleclient;

string sCon = "data source = <oracle service name>;uid=<oracle user id>;pwd=<oracle password>";

oracleconnection oCon = new oracleconnection(sCon);

oraclecommand cmd = new oraclecommand();

cmd.connection = oCon;

cmd.commandText = "<oracle sp name>";

cmd.commandType = commandtype.storedprocedure;

oraclecommandbuilder.deriveparameters(cmd);

foreach(parameter p in cmd.parameters)

  if(p.oracletype == oracletype.cursor) p.parameterdirection = parameterdirection.output;

cmd.parameters["<ora Param Name>"].value = "<value>";

oracledataadapter da = new oracledataadapter(cmd);

dataset dsout = new dataset("Cursor Output");

da.fill(dsout);

finally you will get the cursor output in the dataset.

good luck.

regards,

Raja. K

# December 18, 2008 9:18 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)