Mapping Oracle UDT using c# without stored procedure

2.8k views Asked by At

I am required to map an Oracle Object type in my c# application. There are tons of examples online, both here and other sites/blogs, but all of them include using a stored procedure, which I shouldn't do.

I have been searching for the past two days and the closest I got was an article on docs.oracle.com, but it is without an example.

Can anyone, please, give an example of how this could be achieved?

I am using Oracle.DataAccess class for communication with my database and a simple UDT given below:

create or replace 
TYPE "MYNUMBER_TYPE" AS OBJECT (
  MyNumber NUMBER(13)
)
INSTANTIABLE NOT FINAL;
1

There are 1 answers

4
Sebastian Lerch On BEST ANSWER

If you want to execute PL/SQL you can do something like the following. This is mighty enough to tear the world domination itself. Almost.

Note, this is not tested, as I do not have a Oracle DB here. However I am using this approach in one of my current projects.

cmd = New OracleCommand("declare " +
          "    lSomeVarchar2 varchar2(255); " +
          "    lSomeNumber number; " +
          "    lSomeLong long; " +
          "begin " +
          "  loop " +
          "  --do something fancy here  " +
          "  end loop; " +
          "  --you can pass variables from outside: " +
          " :parameterNumber:= lSomeNumber ; " +
          " :parameterVarChar := lSomeLong; " +
          "end;", conn);
          //make these of direction output and you can get values back
cmd.Parameters.Add("parameterNumber", OracleDbType.Integer).Direction = ParameterDirection.Output;
cmd.Parameters.Add("parameterVarChar", OracleDbType.VarChar).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();

//now you can get the values using something like
int cNumber = (int)cmd.Parameters("parameterNumber").Value;
String myString = (String) cmd.Parameters("parameterNumber").Value;

EDIT 3 aka answer to you comment:

For usage of the IOracleCustomType-Interface: Again, I couldn't test it as I still don't have access to an Oracle database. However, let's do some magic.

Step 1: Create a custom type in your C# code which inherits from IOracleCustomType:

[OracleCustomTypeMapping("C##USER.MYNUMBER_TYPE")]
public class MyCustomClass : IOracleCustomType

Then for each class member you have to specify the Oracle pendant. In the following the name "MyNumber" comes from the custom type specification in your question.

[OracleObjectMappingAttribute("MyNumber")]
public virtual int cNumber{get; set;}

Furthermore you have to override the methods FromCustomObject and ToCustomObject:

//this one is used to map the C# class-object to Oracle UDT
public virtual void FromCustomObject(OracleConnection conn, IntPtr object){
    OracleUdt.SetValue(conn, object, "MyNumber", this.cNumber);
}

//and this one is used to convert Oracle UDT to C# class
public virtual void ToCustomObject(OracleConnection conn, IntPtr object){
    this.cNumber = ((int)(OracleUdt.GetValue(conn, object, "MyNumber")));
}

Step 2: Create the custom type in Database which you already did. So I will not repeat it here.

Step 3: Now we are set up. Let's try it:

//first create your SQL-Statement
String statement = "SELECT MY_CUSTOM_TYPE_COLUMN FROM MY_SUPER_TABLE";

//then set up the database connection
OracleConnection conn = new OracleConnection("connect string");
conn.Open();
OracleCommand cmd = new OracleCommand(statement, conn);
cmd.CommandType = CommandType.Text;

//execute the thing
OracleDataReader reader = cmd.ExecuteReader();

//get the results
while(reader.Read()){
    MyCustomClass customObject = new MyCustomClass();
    //get the Object, here the magic happens
    customObject = (MyCustomClass)reader.GetValue(0);

    //do something with your object

}