How to Set User-Defined-type as Oracle SP input parameter in. net

Source: Internet
Author: User
Tags oracleconnection

From: http://forums.oracle.com/forums/thread.jspa? Threadid = 386484 & tstart = 0
Http://forums.oracle.com/forums/thread.jspa? Threadid = 911710 & tstart = 29

We have an oracle SP that is being called in C #.

Now we try to pass a user defined type (UDT) as an input parameter to the sp;
However we did not find a solution.
It is possible to use ORACLE ref_cursor as SP output (!) Parameters but not input parameters, since ODP. Net does support input parameters until Oracle version 10. (as for ADO. net, we have not found any examples .)

There might be a solution with using Oracle anytype datatype or some other strange but brilliant developer idea; but we still don't know how to proceed ....

Example Oracle-UDT:
Type error_info
As object
(
Code number (5 ),
Description varchar2 (1000)
)

Example SP:
Function sf_test1 (error_in in error_info default null)

Return Boolean;

 

It possible to use user-defined-type as SP input parameter in. net using new odp.net 11.1.6 that support UDT.
Here my sample:
I created simple object type

Create or replace type typ_address as object
(
-- Attributes
Address varcharacter 2 (500 ),
Zipcode varchar2 (5)
)

Then I created simple SP using as input parameter an object in order to map it on relational table:

Create Table tab_address
(
Address varcharacter 2 (200 ),
Zipcode varchar2 (5)
);

Create or replace procedure test_udt_sp (obj_address in typ_address) is
Begin
Insert into tab_address (address, zipcode) values (obj_address.address, obj_address.zipcode );
Commit;
End test_udt_sp;

After I created simple console application using C # And odp.net.
In order to map Oracle object typ_address I created custum class typ_address (this cocould be done automatically with new ODT). Here the C # code autogenerated by ODT:

//------------------------------------------------------------------------------
// <Autogenerated>
// This code was generated by a tool.
// Runtime version: 1.1.4322.2407
//
// Changes to this file may cause incorrect behavior and will be lost if
// The code is regenerated.
/// </Autogenerated>
//------------------------------------------------------------------------------

Using system;
Using Oracle. dataaccess. client;
Using Oracle. dataaccess. types;

Public class typ_address: inullable, ioraclecustomtype {

Private bool m_isnull;

Private string m_zipcode;

Private string m_address;

Public typ_address (){
// Todo: Add code to initialise the object
}

Public typ_address (string Str ){
// Todo: Add code to initialise the object based on the given string
}

Public Virtual bool isnull {
Get {
Return this. m_isnull;
}
}

Public static typ_address null {
Get {
Typ_address OBJ = new typ_address ();
OBJ. m_isnull = true;
Return OBJ;
}
}

Public String zipcode {
Get {
Return this. m_zipcode;
}
Set {
This. m_zipcode = value;
}
}

Public String address {
Get {
Return this. m_address;
}
Set {
This. m_address = value;
}
}

Public Virtual void fromcustomobject (Oracle. dataaccess. Client. oracleconnection con, system. intptr pudt ){
Oracle. dataaccess. types. oracleudt. setvalue (con, pudt, "zipcode", this. zipcode );
Oracle. dataaccess. types. oracleudt. setvalue (con, pudt, "Address", this. Address );
}

Public Virtual void tocustomobject (Oracle. dataaccess. Client. oracleconnection con, system. intptr pudt ){
This. zipcode = (string) (Oracle. dataaccess. types. oracleudt. getvalue (con, pudt, "zipcode ")));
This. Address = (string) (Oracle. dataaccess. types. oracleudt. getvalue (con, pudt, "Address ")));
}

Public override string tostring (){
// Todo: return a string that represents the current object
Return "";
}

Public static typ_address parse (string Str ){
// Todo: Add code needed to parse the string and get the object represented by the string
Return new typ_address ();
}
}

// Factory to create an object for the above class

Public class typ_addressfactory: ioraclecustomtypefactory {

Public Virtual ioraclecustomtype Createobject (){
Typ_address OBJ = new typ_address ();
Return OBJ;
}
}

Then I wrote simple C # code to call the SP using the UDT:

Using system;
Using system. Data;
Using Oracle. dataaccess. client;
Using Oracle. dataaccess. types;

Class objectsample
{
Static void main (string [] ARGs)
{
// Cahnge your connection string
String constr = "User ID = XXXXX; Password = xxxxxx; Data Source = xxxxxx ";

Typ_address ADDR = new typ_address ();
ADDR. Address = "Via Emilia ponente 216 ";
ADDR. zipcode = "40133 ";

// Establish a connection to Oracle
Oracleconnection con = new oracleconnection (constr );
Con. open ();

// Call SP passing object ADDR
// The SP map the object on relational table
Oraclecommand cmd = new oraclecommand ();
Cmd. Connection = con;
Cmd. commandtype = commandtype. storedprocedure;
Cmd. commandtext = "test_udt_sp ";
Oracleparameter param1 = new oracleparameter ();

Param1.oracledbtype = oracledbtype. object;
Param1.direction = parameterdirection. InputOutput;

// Note: The udttypename is case-senstive
Param1.udttypename = "typ_address ";
Param1.value = ADDR;

Cmd. Parameters. Add (param1 );

Try
{

Cmd. executenonquery ();

}
Catch (exception exc)
{
Console. writeline (EXC. Message );
}
Finally
{

Cmd. Dispose ();
Con. Close ();
Con. Dispose ();
}
}

}

 

For example:
You have table of typ_address and use this as output-parameter.

So I wocould code it like that -- but it doesn' t work
...
Oracleparameter param1 = new oracleparameter ();

Param1.oracledbtype = oracledbtype. object;
Param1.collectiontype = oraclecollectiontype. plsqlassociativearray;
Param1.direction = parameterdirection. output;
Param1.size = 3; // means 3 fields in the array

// Note: The udttypename is case-senstive
Param1.udttypename = "typ_address ";
Param1.value = NULL;
...
Cmd. executenonquery (); // -->
// Get an exception: Invalid Number or types for the parameters

Hope, you can help me a little bit-Thx in advance

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.