Oracle Stored Procedure and custom data type(Oracle 存储过程和自定义数据类型)
问题描述
I have an Oracle stored procedure which takes two parameters: a custom data type and a string.
Calling the stored procedure in Oracle, I would do the following:
EXECUTE MY_STORED_PROCEDURE(MYTYPE_T(99, 231), 'mystring')
How can I execute this using C#? I understand that I need to setup the command to be a stored procedure, but how do I specify the first parameter as custom data type?
Update:
MYTYPE_T
is TABLE OF NUMBER
created via
CREATE OR REPLACE TYPE mytype_t AS TABLE OF NUMBER ;
You won't be able to do this easily with the deprecated System.Data.OracleClient but you can utilize oracle's ODP with using UDTs. If that is not an option, I am unsure how you can do it via parameters in C# with System.Data.
ODP does come with a lot of examples and there are examples in the above links.
I am going to add some more links that will hopefully help:
- visual studio ODP index
- this shows you exactly how to utilize the ODT to create you custom class wrappers and call them (do note that this is midway through, they walk through using the tool to create the custom types above it in the example -- this walkthrough is quite thorough and should get you directly where you need to be)
- Download: now this guy also installs sample files, this is another terrific example of exactly what you need to do: once installed goto [directory path you install]..product11.2.0client_1odp.netsamples4UDTobject1.cs
It really pays to allow the ODT tools for Visual studio to create your classes for your UDTs for you (e.g. IOracleCustomType and such) . you can then go into them and amend them to suit your needs. then once all is said and done (snippet from object1.cs):
Person p1 = new Person();
p1.Name = "John";
p1.Address = "Address1";
p1.Age = 20;
// Establish a connection to Oracle
OracleConnection con = new OracleConnection(constr);
con.Open();
// Update Person object and insert it into a database table
OracleCommand cmd = new OracleCommand(sql1, con);
cmd.CommandType = CommandType.StoredProcedure;
OracleParameter param1 = new OracleParameter();
param1.OracleDbType = OracleDbType.Object;
param1.Direction = ParameterDirection.InputOutput;
// Note: The UdtTypeName is case-senstive
param1.UdtTypeName = "SCOTT.ODP_OBJ1_SAMPLE_PERSON_TYPE";
param1.Value = p1;
cmd.Parameters.Add(param1);
also note that Person class must implement IOracleCustomType (which can be created by following the link in #2)
/* Person Class
An instance of a Person class represents an ODP_OBJ1_SAMPLE_PERSON_TYPE object
A custom type must implement INullable and IOracleCustomType interfaces
*/
public class Person : INullable, IOracleCustomType
The above is for a full custom type, but you are after an associative array ODP binding:
http://weblogs.asp.net/ricardoperes/archive/2009/05/14/odp-net-associative-arrays.aspx
you'll want to use
param1.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
and everything should fall into place
这篇关于Oracle 存储过程和自定义数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Oracle 存储过程和自定义数据类型
基础教程推荐
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- rabbitmq 的 REST API 2022-01-01