我正在尝试使用ODP.NET在我的代码中使用UDT。我见过几个例子,但是当我运行我的代码时,我得到一个错误,说无效的参数名称绑定。无法找出代码失败的位置。请在下面找到代码。
Oracle部分:
create or replace
TYPE T_SALES_PATTERN is object (siteID varchar2(10),
tankID varchar2(10),
dayNo integer,
periodID integer,
startTime varchar2(4),
endTime varchar2(4),
intraDayPctRate number(3,2),
totalDailySalesPctRate number(3,2),
averageDailySales integer,
ouID varchar2(3));
create or replace
TYPE T_SALES_PATTERN_TABLE is table of T_SALES_PATTERN;
function insertVMIFESalesPatterns( p_vmifeSalesPatterns T_SALES_PATTERN_TABLE) return integer is
begin
for recs in (select * from TABLE(p_vmifeSalesPatterns))
loop
dbms_output.put_line(recs.siteID);
end loop;
commit;
return g_SUCCESS;
exception when others then
return g_ERROR;
end;
C#代码: 销售模式类
public class SalesPattern : INullable, IOracleCustomType
{
private bool objectIsNull;
[OracleObjectMappingAttribute("SITE_ID")]
public string SiteId { get; set; }
[OracleObjectMappingAttribute("TANK_ID")]
public string TankId { get; set; }
[OracleObjectMappingAttribute("DAYNO")]
public long? DayNo { get; set; }
[OracleObjectMappingAttribute("PERIODID")]
public long? PeriodId { get; set; }
[OracleObjectMappingAttribute("STARTTIME")]
public string StartTime { get; set; }
[OracleObjectMappingAttribute("ENDTIME")]
public string EndTime { get; set; }
[OracleObjectMappingAttribute("INTRADAYPCTRATE")]
public decimal? IntraDayPctRate { get; set; }
[OracleObjectMappingAttribute("TOTALDAILYSALESPCTRATE")]
public decimal? TotalDailySalesPctRate { get; set; }
[OracleObjectMappingAttribute("AVERAGEDAILYSALES")]
public long? AverageDailysales { get; set; }
[OracleObjectMappingAttribute("OU_ID")]
public string OuId { get; set; }
[OracleObjectMappingAttribute("LASTUPDATETIMESTAMP")]
public DateTime LastUpdateTimeStamp { get; set; }
public static SalesPattern Null
{
get
{
SalesPattern salesPattern = new SalesPattern();
salesPattern.objectIsNull = true;
return salesPattern;
}
}
#region INullable Members
public bool IsNull
{
get { return objectIsNull; }
}
public void FromCustomObject(Oracle.DataAccess.Client.OracleConnection con, IntPtr pUdt)
{
// Convert from the Custom Type to Oracle Object
if (!string.IsNullOrEmpty(SiteId))
{
OracleUdt.SetValue(con, pUdt, "SITE_ID", SiteId);
}
if (!string.IsNullOrEmpty(TankId))
{
OracleUdt.SetValue(con, pUdt, "TANK_ID", TankId);
}
if (DayNo != null)
{
OracleUdt.SetValue(con, pUdt, "DAYNO", DayNo);
}
if (PeriodId != null)
{
OracleUdt.SetValue(con, pUdt, "PERIODID", PeriodId);
}
if (!string.IsNullOrEmpty(StartTime))
{
OracleUdt.SetValue(con, pUdt, "STARTTIME", StartTime);
}
if (!string.IsNullOrEmpty(EndTime))
{
OracleUdt.SetValue(con, pUdt, "ENDTIME", EndTime);
}
if (IntraDayPctRate != null)
{
OracleUdt.SetValue(con, pUdt, "INTRADAYPCTRATE", IntraDayPctRate);
}
if (TotalDailySalesPctRate != null)
{
OracleUdt.SetValue(con, pUdt, "TOTALDAILYSALESPCTRATE", TotalDailySalesPctRate);
}
if (AverageDailysales != null)
{
OracleUdt.SetValue(con, pUdt, "AVERAGEDAILYSALES", AverageDailysales);
}
if (!string.IsNullOrEmpty(OuId))
{
OracleUdt.SetValue(con, pUdt, "OU_ID", OuId);
}
}
public void ToCustomObject(Oracle.DataAccess.Client.OracleConnection con, IntPtr pUdt)
{
SiteId = (string)OracleUdt.GetValue(con, pUdt, "SITE_ID");
TankId = (string)OracleUdt.GetValue(con, pUdt, "TANK_ID");
DayNo = (long)OracleUdt.GetValue(con, pUdt, "DAYNO");
PeriodId = (long)OracleUdt.GetValue(con, pUdt, "PERIODID");
StartTime = (string)OracleUdt.GetValue(con, pUdt, "STARTTIME");
EndTime = (string)OracleUdt.GetValue(con, pUdt, "ENDTIME");
IntraDayPctRate = (decimal)OracleUdt.GetValue(con, pUdt, "INTRADAYPCTRATE");
TotalDailySalesPctRate = (decimal)OracleUdt.GetValue(con, pUdt, "TOTALDAILYSALESPCTRATE");
AverageDailysales = (long)OracleUdt.GetValue(con, pUdt, "AVERAGEDAILYSALES");
OuId = (string)OracleUdt.GetValue(con, pUdt, "OU_ID");
}
#endregion
}
[OracleCustomTypeMappingAttribute("T_SALES_PATTERN")]
public class SalesPatternFactory : IOracleCustomTypeFactory
{
#region IOracleCustomTypeFactory Members
public IOracleCustomType CreateObject()
{
return new SalesPattern();
}
#endregion
}
/* SalesPatternList Class
** An instance of a SalesPatternList class represents an SalesPatternList object
** A custom type must implement INullable and IOracleCustomType interfaces
*/
public class SalesPatternList : INullable, IOracleCustomType
{
[OracleArrayMapping()]
public SalesPattern[] SalesPatternArray;
private bool objectIsNull;
#region INullable Members
public bool IsNull
{
get { return objectIsNull; }
}
public static SalesPatternList Null
{
get
{
SalesPatternList obj = new SalesPatternList();
obj.objectIsNull = true;
return obj;
}
}
#endregion
#region IOracleCustomType Members
public void FromCustomObject(Oracle.DataAccess.Client.OracleConnection con, IntPtr pUdt)
{
OracleUdt.SetValue(con, pUdt, 0, SalesPatternArray);
}
public void ToCustomObject(Oracle.DataAccess.Client.OracleConnection con, IntPtr pUdt)
{
SalesPatternArray = (SalesPattern[])OracleUdt.GetValue(con, pUdt, 0);
}
#endregion
}
[OracleCustomTypeMapping("T_SALES_PATTERN_TABLE")]
public class SalesPatternListFactory : IOracleCustomTypeFactory, IOracleArrayTypeFactory
{
#region IOracleCustomTypeFactory Members
public IOracleCustomType CreateObject()
{
return new SalesPatternList();
}
#endregion
#region IOracleArrayTypeFactory Members
public Array CreateArray(int numElems)
{
return new SalesPattern[numElems];
}
public Array CreateStatusArray(int numElems)
{
return null;
}
#endregion
}
ODP.NET代码:
public long InsertSalesPattern(List<SalesPattern> salesList)
{
try
{
logger.Info("Entering insert VMI Sales Pattern method");
SalesPatternList salesPatternList = new SalesPatternList();
salesPatternList.SalesPatternArray = salesList.ToArray();
using (OracleConnection cn = new OracleConnection(connection))
{
cmd = new OracleCommand("X4_DOM.insertVMIFESalesPatterns");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = cn;
cmd.Parameters.Add("Return_Value", OracleDbType.Int16, ParameterDirection.ReturnValue);
OracleParameter parameter = new OracleParameter();
parameter.ParameterName = "p_vmifeSalesPatterns";
parameter.OracleDbType = OracleDbType.Array;
parameter.UdtTypeName = "T_SALES_PATTERN_TABLE";
parameter.Value = salesPatternList;
cmd.Parameters.Add(parameter);
cn.Open();
cmd.ExecuteNonQuery();
string result = cmd.Parameters["Return_Value"].Value.ToString();
logger.Info("VMI Sales Pattern");
return result == "0" ? 0 : 1;
}
}
catch (OracleException e)
{
logger.Error("The Following error has occured: -" + e.Message);
return -1;
}
catch (Exception ex)
{
logger.Error("The Following error has occured: -" + ex.Message);
return -1;
}
}