建议使用OracleCustomTypeMapping属性和OracleObjectMapping属性。我不记得我从哪里得到这个想法了,但是...
public interface INullableOracleCustomType: INullable, IOracleCustomType
{
}
[OracleCustomTypeMapping("<YOUR_SCHEMA_NAME>.<UDT_OBJECT_NAME>")]
public class ParameterObject : INullableOracleCustomType
{
private bool objectIsNull;
#region constructor
public ParameterObject()
{ }
public ParameterObject(string parameterName, string parameterValue)
{
this.ParameterName = parameterName;
this.ParameterValue = parameterValue;
}
#endregion
#region properties
[OracleObjectMappingAttribute("PARAMETERNAME")]
public string ParameterName { get; set; }
[OracleObjectMappingAttribute("PARAMETERVALUE")]
public string ParameterValue { get; set; }
public static ParameterObject Null
{
get
{
ParameterObject parameterObject = new ParameterObject();
parameterObject.objectIsNull = true;
return parameterObject;
}
}
#endregion
#region INullable Members
public bool IsNull
{
get { return objectIsNull; }
}
#endregion
#region IOracleCustomType
public void FromCustomObject(Oracle.DataAccess.Client.OracleConnection con, IntPtr pUdt)
{
if (!string.IsNullOrEmpty(ParameterName))
{
OracleUdt.SetValue(con, pUdt, "PARAMETERNAME", ParameterName);
}
if (!string.IsNullOrEmpty(ParameterValue))
{
OracleUdt.SetValue(con, pUdt, "PARAMETERVALUE", ParameterValue);
}
}
public void ToCustomObject(Oracle.DataAccess.Client.OracleConnection con, IntPtr pUdt)
{
ParameterName = (string)OracleUdt.GetValue(con, pUdt, "PARAMETERNAME");
ParameterValue = (string)OracleUdt.GetValue(con, pUdt, "PARAMETERVALUE");
}
#endregion
}
示例用法...
public static OracleParameter CreateCustomTypeArrayInputParameter<T>(string name, string oracleUDTName, T value)
where T : INullableOracleCustomType
{
OracleParameter parameter = new OracleParameter();
parameter.ParameterName = name;
parameter.OracleDbType = OracleDbType.Array;
parameter.Direction = ParameterDirection.Input;
parameter.UdtTypeName = oracleUDTName;
parameter.Value = value;
return parameter;
}
Oracle似乎对模式/类型/对象名称必须使用大写字母感到挑剔。
Query
都会产生一个ArgumentException
(ODAC set_value)。但这可能是由于使用了经典的ODAC程序集。我已经用经典的ODP.NET的Oracle.DataAccess.Client
替换了Oracle.ManagedDataAccess.Client
。托管的ODP驱动程序仍处于测试版。 - Marco Mayer