简体   繁体   English

C#使用嵌套UDT调用Oracle存储过程

[英]C# Call Oracle Stored Procedure with nested UDT

Is it possible to call a stored procedure with an nested object as a parameter? 是否可以使用嵌套对象作为参数来调用存储过程?

For example I have a Person class that has an Address instance. 例如,我有一个具有Address实例的Person类。

Person p1   = new Person();
p1.Name     = "John";
p1.Address  = new Adress{Street = “StackOverflow”, App = 3}
p1.Age = 20;

In the database I have created the user defined types, and the stored procedure that takes as parameter a person_type that contains an address_type. 在数据库中,我创建了用户定义的类型,并创建了一个存储过程,该存储过程将包含address_type的person_type作为参数。

create or replace 
TYPE Person_TYPE AS OBJECT (
    Name     VARCHAR2(100),
    Age      NUMBER,
    OBJ_Address  Adress_TYPE
);

In the SP I have: 在SP中,我有:

function  FC_Insert_Person  ( DATAINS  Person_TYPE ) 
    BEGIN

       INSERT INTO Person ( Name, Age ) VALUES (DATAINS.Name, DATAINS.Age )

     --insert into the nested object
     FC_INSERT_Addrres (DATAINS.OBJ_Address);
     /* in the adress sp I get the id of the person using an SEQ_PERSON_ID.CURRVAL; */

return ...
end FC_Insert_Person;

All the code examples that I find are only for simple objects, or a collection of tables so this makes me ask myself if this will be possible. 我找到的所有代码示例仅适用于简单对象或表的集合,因此这使我问自己是否可行。

OracleParameter[] param = new OracleParameter[1];
                param[0] = new OracleParameter(my_type, OracleDbType.Object, ParameterDirection.Input);
                param[0].UdtTypeName = "Person_TYPE";
                param[0].Value = p1;
                dObj.ExecuteCommand("FC_Insert_Person", param);

Yes. 是。 Use the custom class wizard which is part of Oracle Developer Tools for Visual Studio. 使用自定义类向导,该向导是Oracle Developer Studio for Visual Studio的一部分。 A walkthough example is here: 这里有一个示例:

http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/appdev/dotnet/userdefinedtypes/index.html http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/appdev/dotnet/userdefinedtypes/index.html

Note that it is not advisable to rely on UDTs if performance is critical (eg lots of data is being passed). 请注意,如果性能至关重要(例如,正在传递大量数据),则不建议依赖UDT。 In that case you should flatten the objects and use associative arrays...or use temporary tables. 在这种情况下,您应该展平对象并使用关联数组...或使用临时表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM