Click here to Skip to main content
13,556,447 members
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 1 Jan 2011
Licenced CPOL

Interaction between C# Application and Oracle through Custom Object

, 1 Jan 2011
Rate this:
Please Sign up or sign in to vote.
Interaction between C# application and Oracle through custom Object


As a developer, I am very much fond of OOPS and its implementation in C#. When we talk about OOPS, most of us are quite comfortable to play with custom object (user defined object) and transport them across different application layer. The real pain comes when we plan to send or retrieve custom object to/from database. More specifically, this is really a challenging task to achieve database communication through C# entity.

In my career, I mostly work with Oracle and C#. As we know, both of these platforms are object oriented so I decided to put into practice the OOPS approach for DB communication.

After a long struggle and digging into various available options, I found ODP.NET allows interaction to database in terms object passing.

Here in this example, I referred to ODP.NET (Oracle Data Provider for .NET, Release – 11.1), Oracle 10g and Visual Studio 2008.

ODP.NET is freely available and one can download the executable from Oracle site at

Below, I am going to discuss the implementation steps in detail.

Send Custom Object to Oracle Stored Procedure

//Person Entity – C# Custom Object
PersonBO objPersonBO 	= new PersonBO();
objPersonBO.Address 	= "Kolkata";
objPersonBO.Age 		= 20;
objPersonBO.Name 		= "Mr.Jhon";

//------ Establish the connection with Oracle-----///

//Insert the Person object into database table
OracleCommand cmd = new OracleCommand("ODP_RND_InsertPerson_Proc", objCon);
cmd.CommandType = CommandType.StoredProcedure; //Database store procedure

//Oracle Parameter
OracleParameter objParam = new OracleParameter();

//Denotes, we are going to pass a custom object
objParam.OracleDbType = OracleDbType.Object;

objParam.Direction = ParameterDirection.Input;

//Note: The UdtTypeName is case-sensitive - Should be in upper case
//This is a database object and physically exists in the database as custom // type
objParam.UdtTypeName = "ODP_RND_PERSON_TYPE";

//Attach the C# custom object as input parameter
objParam.Value = objPersonBO;

//Attach parameter to command object

//Insert the UDT into the table

From the above code snippet, we come across a new keyword “UdtTypeName” which refers to Oracle user type. We will explain this later on in the discussion.

Receive Data as Custom Object from Oracle Store Procedure

This requires few steps to fetch the data from database.

//SQL statement
string strSql = "SELECT FROM odp_rnd_person_table c"

//------ Establish the connection with Oracle-----///

//Pass the SQL statement
OracleCommand objCmd 		= new OracleCommand(strSql, objCon);
objCmd.CommandType 		= CommandType.Text;

//Issue the statement
OracleDataReader objReader 	= objCmd.ExecuteReader();

//Fetch each row
while (objReader.Read())
      //Custom object
      PersonBO objPersonBO 	= new PersonBO();

       //Fetch the objects as a custom type
      objPersonBO 		= (PersonBO)objReader.GetValue(0);

We are done with data exchange between C# and Oracle which requires nominal steps to be performed. More interesting part we are going to discuss is the custom object creation.

Namespaces are required:

using System;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
using System.Xml.Serialization;
using System.Xml.Schema;

Create the custom class and make it derive from IOracleCustomType. IOracleCustomType is an interface for conversion between C# Custom Type and an Oracle Object Type.

public class PersonBO :  IOracleCustomType

Create the following public property underneath PersonBO and make it decorated by OracleObjectMappingAttribute.The OracleObjectMappingAttribute needs to be specified on each members of custom type that represent the Oracle object type. This attribute must specify the name or zero-based index of the attribute in the Oracle object that the custom class property maps to. This also allows the custom type to declare field or property names which differ from the Oracle Object type.

 public virtual string Name{get;set;}
 public virtual string Address{get;set;}
 public virtual decimal Age { get; set; }

Create the following method FromCustomObject underneath PersonBO and override it. This interface method creates an Oracle Object by setting the attribute respectively on the specified Oracle UDT.

public virtual void FromCustomObject(OracleConnection objCon, IntPtr objUdt)
    //The FromCustomObject method is used to build an Oracle Object or   
    //Collection from a custom object by 
    //setting attribute or element values respectively through the 
    //OracleUdt.SetValue method.

    OracleUdt.SetValue(objCon, objUdt, "PNAME", this.Name);
    OracleUdt.SetValue(objCon, objUdt, "ADDRESS", this.Address);
    if (this.Age > 0) OracleUdt.SetValue(objCon, objUdt, "AGE", this.Age);

Create the following method ToCustomObject underneath PersonBO and override it. It provides the Oracle Object with the attribute values to set on the custom type. This interface method initializes a custom object using the specified Oracle UDT.

public virtual void ToCustomObject(OracleConnection objCon, IntPtr objUdt)
   //The ToCustomObject method is used to initialize a custom object from the 
   //specified Oracle 
   //Object or Collection by retrieving attribute or element values 
   //respectively through the OracleUdt.GetValue method.
    this.Name    = ((string)(OracleUdt.GetValue(objCon, objUdt, "PNAME")));
    this.Address = ((string)(OracleUdt.GetValue(objCon, objUdt, "ADDRESS")));

    bool AgeIsNull = OracleUdt.IsDBNull(objCon, objUdt, "AGE");
    if ((AgeIsNull == false)) this.Age = 
		((decimal)(OracleUdt.GetValue(objCon, objUdt, "AGE")));

Prepare the Database Object


The field CONTACT in the above script is type of ODP_RND_PERSON_TYPE which is an Oracle user defined type.

CREATE OR REPLACE type ODP_RND_Person_Type as object 
     pname varchar2(30), 
     address varchar2(60), 
     age number(3)

Here, we must remember the structure of C# and Oracle user define type should be identically same.

Here is the DB stored procedure for inserting data into table. This procedure accepts the above type as input parameter. This type encapsulates the actual value passed from UI level.

Within the procedure, if we want to access the value of individual property, we can do it in the following way:

Person.pname, person.address, etc.

 ODP_RND_InsertPerson_Proc(person IN ODP_RND_Person_Type) as
    Insert into ODP_RND_Person_Table values (person); 

!! Enjoy coding !!


  • 1st January, 2011: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Bibhas Paul
Software Developer (Senior) Reputed MNC in Kolkata
India India
Bibhas has 8 years of extensive experience in application development with exposure to business requirement study, system analysis and designing, coding ,testing,
implementation, end user training and client Interaction.

He was the part of application development team and worked for industry leading organizations like "ConocoPhillips", "Abbey National Bank" and “DHL".

Posses sound experience as a technical architect and all part of software development lifecycle.

His interest includes in Microsoft Technologies (ASP.NET 3.5 & SQL Server), Design Pattern and wide variety of internet technologies like AJAX, JQUERY etc.

You may also be interested in...


Comments and Discussions

GeneralThanks. It is very useful. Pin
Member 127122889-Mar-17 2:19
memberMember 127122889-Mar-17 2:19 
QuestionAdding list of my custom type Pin
OsamaBakr17-May-15 15:07
memberOsamaBakr17-May-15 15:07 
QuestionCustom Class Attribute Pin
Member 986640730-Oct-14 11:14
memberMember 986640730-Oct-14 11:14 
QuestionArray within the UDT Pin
Dishan Fernando19-Mar-14 19:42
memberDishan Fernando19-Mar-14 19:42 
GeneralTHanks Pin
Member 881492620-Apr-12 0:33
memberMember 881492620-Apr-12 0:33 
Questionwhile creating the below procedure error message is coming as procedure created with compilation errors Pin
Member 881492618-Apr-12 20:34
memberMember 881492618-Apr-12 20:34 
AnswerThanks..... Pin
Member 881492616-Apr-12 23:34
memberMember 881492616-Apr-12 23:34 
GeneralThanks... Pin
SOE NAING2228-May-11 11:12
memberSOE NAING2228-May-11 11:12 
GeneralMy vote of 5 Pin
Prosanta Kundu online11-Jan-11 16:55
memberProsanta Kundu online11-Jan-11 16:55 
GeneralVery Nice invention. Pin
Ravi LVS7-Jan-11 3:53
memberRavi LVS7-Jan-11 3:53 
GeneralRe: Very Nice invention. Pin
Bibhas Paul9-Jan-11 16:50
memberBibhas Paul9-Jan-11 16:50 
Generalthanks for sharing - have 5 Pin
Pranay Rana5-Jan-11 21:00
memberPranay Rana5-Jan-11 21:00 
GeneralRe: thanks for sharing - have 5 Pin
Bibhas Paul9-Jan-11 16:50
memberBibhas Paul9-Jan-11 16:50 
QuestionRelationsships Pin
WuschWuschi3-Jan-11 6:15
memberWuschWuschi3-Jan-11 6:15 
AnswerRe: Relationsships Pin
Bibhas Paul3-Jan-11 16:43
memberBibhas Paul3-Jan-11 16:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180515.1 | Last Updated 1 Jan 2011
Article Copyright 2011 by Bibhas Paul
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid