我试图让C#读取PL / SQL用户定义类型的数组。
这是数组中每个对象的定义:
CREATE OR REPLACE TYPE "REPORTDATUM" AS OBJECT
(
FIELD1 VARCHAR2(20),
FIELD2 VARCHAR2(3000),
FIELD3 NUMBER(15),
FIELD4 DATE,
FIELD5 DATE,
FIELD6 VARCHAR2(4000),
FIELD7 TIMESTAMP,
FIELD8 VARCHAR2(10),
FIELD9 NUMBER(35),
FIELD10 VARCHAR2(80),
FIELD11 VARCHAR2(50)
);
CREATE OR REPLACE TYPE "REPORTARRAY" IS TABLE OF REPORTDATUM;
这是我要调用的函数的签名:
FUNCTION getReport(startTime IN DATE, endTime IN DATE) RETURN REPORTARRAY;
现在,根据我在此处找到的指南:http://appsjack.blogspot.com/2010/09/pass-custom-udt-types-to-oracle-stored.html
我按如下所示创建了ReportDatum.cs类:
public class ReportDatum : INullable, IOracleCustomType {
private bool objectIsNull;
[OracleObjectMappingAttribute(Constants.ReportDatum.Aliases.Field1)]
public string Field1 { get; set; }
[OracleObjectMappingAttribute(Constants.ReportDatum.Aliases.Field2)]
public string Field2 { get; set; }
[...]
[OracleObjectMappingAttribute(Constants.ReportDatum.Aliases.Field11)]
public string Field11 { get; set; }
public static ReportDatum Null {
get {
ReportDatum reportDatum = new ReportDatum();
reportDatum.objectIsNull = true;
return reportDatum;
}
}
public bool IsNull {
get { return objectIsNull; }
}
public void FromCustomObject(OracleConnection databaseConnection, IntPtr userDefinedTypePointer) {
if (string.IsNullOrEmpty(this.Field1) == false ) {
OracleUdt.SetValue(databaseConnection, userDefinedTypePointer, Constants.ReportDatum.Aliases.Field1, this.Field1);
}
if (string.IsNullOrEmpty(this.Field2) == false) {
OracleUdt.SetValue(databaseConnection, userDefinedTypePointer, Constants.ReportDatum.Aliases.Field2, this.Field2);
}
[...]
if (string.IsNullOrEmpty(this.Field11) == false) {
OracleUdt.SetValue(databaseConnection, userDefinedTypePointer, Constants.ReportDatum.Aliases.Field11, this.Field11);
}
}
public void ToCustomObject(OracleConnection databaseConnection, IntPtr userDefinedTypePointer) {
this.Field1 = parseString(databaseConnection, userDefinedTypePointer, Constants.ReportDatum.Aliases.Field1);
this.Field2 = parseString(databaseConnection, userDefinedTypePointer, Constants.ReportDatum.Aliases.Field2);
[...]
this.Field11 = parseString(databaseConnection, userDefinedTypePointer, Constants.ReportDatum.Aliases.Field11);
}
private string parseString(OracleConnection databaseConnection, IntPtr userDefinedTypePointer, string columnName) {
string valueToReturn = String.Empty;
try {
valueToReturn = ( String ) OracleUdt.GetValue(databaseConnection, userDefinedTypePointer, columnName);
}
catch (Exception e) {
error("Exception while attempting to parse string value of column [" + columnName + "]. Message [" + (e == null || e.Message == null ? "NULL" : e.Message) + "]", e);
}
return valueToReturn;
}
}
我还按照如下步骤创建ReportDatumFactory.cs:
public class ReportDatumFactory : IOracleCustomTypeFactory {
public IOracleCustomType CreateObject() {
return new ReportDatum();
}
}
根据我上面链接的指南,这应该足以处理组成数组的每个项目。
为了处理数组本身,我创建了以下类。
ReportArray.cs
public class ReportArray : INullable, IOracleCustomType {
[OracleArrayMapping()]
public ReportDatum[] reportArray;
private bool objectIsNull;
public bool IsNull {
get { return objectIsNull; }
}
public void FromCustomObject(OracleConnection databaseConnection, IntPtr userDefinedTypePointer) {
try {
OracleUdt.SetValue(databaseConnection, userDefinedTypePointer, 0, reportArray);
}
catch (Exception e) {
error("Exception while populating OracleUdt from ReportArray.", e);
}
}
public void ToCustomObject(OracleConnection databaseConnection, IntPtr userDefinedTypePointer) {
try {
reportArray = ( ReportDatum[] ) OracleUdt.GetValue(databaseConnection, userDefinedTypePointer, 0);
}
catch (Exception e) {
error("Exception while reading values from OracleUdt related to ReportArray.", e);
}
}
}
以及关联的工厂类
ReportArrayFactory.cs
public class ReportArrayFactory : IOracleCustomTypeFactory, IOracleArrayTypeFactory {
public IOracleCustomType CreateObject() {
return new ReportArray();
}
public Array CreateArray(int numberOfElements) {
return new ReportArray[numberOfElements];
}
public Array CreateStatusArray(int numberOfElements) {
return null;
}
}
现在,关于调用存储过程的方法,这就是我所拥有的:
Report.cs
private void executeStoredProcedure2(OracleConnection databaseConnection, string schema, string storedProcedureName, DateTime startTime, DateTime endTime, int numberOfElements) {
OracleCommand databaseCommand = new OracleCommand();
ReportArray reportArray = new ReportArray();
debug("Attempting to populate the report array");
if (databaseConnection != null && databaseConnection.State == ConnectionState.Open) {
try {
databaseCommand.Connection = databaseConnection;
databaseCommand.CommandType = CommandType.StoredProcedure;
databaseCommand.CommandText = schema + "." + storedProcedureName;
databaseCommand.Parameters.Add(createUserDefinedParameter(numberOfElements, reportArray));
databaseCommand.Parameters.Add(Constants.ReportDatum.ParameterNames.startTime, OracleDbType.Date, startTime, ParameterDirection.Input);
databaseCommand.Parameters.Add(Constants.ReportDatum.ParameterNames.endTime, OracleDbType.Date, endTime, ParameterDirection.Input);
databaseCommand.ExecuteNonQuery();
//How do I populate the reportArray object?
}
catch (Exception exception) {
error("Exception while executing stored procedure [" + storedProcedure2 + "].", exception);
}
finally {
databaseCommand.Dispose();
}
}
}
private OracleParameter createUserDefinedParameter(int size, ReportArray reportArray) {
OracleParameter valueToReturn = null;
try {
valueToReturn = new OracleParameter();
valueToReturn.ParameterName = "reportData"; //this is the name of the REPORTARRAY object in the Oracle function that is being returned.
valueToReturn.OracleDbType = OracleDbType.Array;
valueToReturn.Direction = ParameterDirection.ReturnValue;
valueToReturn.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
valueToReturn.UdtTypeName = Constants.ReportDatum.arrayDataTypeDefinition;
valueToReturn.ArrayBindSize = new int[size];
valueToReturn.ArrayBindStatus = new OracleParameterStatus[size];
valueToReturn.Size = size;
valueToReturn.Value = reportArray.reportArray;
}
catch (Exception e) {
error("Exception while attempting to create user defined parameter.", e);
}
return valueToReturn;
}
我面临的问题是我无法使用C#返回数组。有人遇到过这个问题吗?我想念什么?
我有理由相信SQL代码可以正常工作。目前只有C#造成了问题。
答案 0 :(得分:0)
似乎有些错误。
首先,工厂需要OracleCustomTypeMapping
属性。没有它们,Oracle将根本不会使用这些映射工厂类:
[OracleCustomTypeMapping("SCHEMA.REPORTDATUM")]
public class ReportDatum ...
和
[OracleCustomTypeMapping("SCHEMA.REPORTARRAY")]
public class ReportArray ...
第二,数组映射工厂中存在一个错误:创建数组时,您想要创建ReportDatum
对象的数组。目前,您正在创建ReportArray
对象的数组,即不需要的数组。换句话说,替换
public Array CreateArray(int numberOfElements) {
return new ReportArray[numberOfElements];
}
使用
public Array CreateArray(int numberOfElements) {
return new ReportDatum[numberOfElements];
}
第三,删除行
CollectionType = OracleCollectionType.PLSQLAssociativeArray,
来自您的createUserDefinedParameter
方法。返回的值不是PL / SQL关联数组(它是另一种集合),因此不应使用此属性。如果使用此属性,则可能会发现从存储的函数返回的返回值是一个空的object
数组。
最后,要从存储的函数调用中取回数组,请在对databaseCommand.ExecuteNonQuery()
的调用之后添加以下行:
reportArray = (ReportArray)databaseCommand.Parameters["reportData"].Value;