在 odp.net 上使用带有 number 属性的 oracle 对象类型
Posted
技术标签:
【中文标题】在 odp.net 上使用带有 number 属性的 oracle 对象类型【英文标题】:using oracle object type with number attribute on odp.net 【发布时间】:2013-06-17 12:10:12 【问题描述】:使用odp.net将Oracle对象类型映射为C#中的类后,将类传回存储过程以将数据插入表中,所有具有数字数据的属性为空。我们没有收到任何例外。附上代码如下。 我正在使用 oracle 9i 和 asp.net v2。
新观察: 我观察到,如果我的变量的类型是 number(1),它会以与下面相同的结构正确插入,但对于其他情况,如 Number、Number(4)、Number(3,2),所有值都是 null。
在下面的示例中,插入后下面的列是空的:
-
ON_ORDER
ON_VERSION
ON_VERSION1
ON_VERSION
但其他变量已完全插入。
当我在 oracle DB 中读取发送项目到我的函数时,不包括数字。 类型结构:
创建或替换类型 CMMT_OBJECTS_TYPE 是对象( OV_FIX_REF_NO VARCHAR2(40), ON_ORDER NUMBER(4), OV_OBJECT_NAME VARCHAR2(100), OV_CATEGORY VARCHAR2(12), OV_SCOPE VARCHAR2(12), OV_TYPE VARCHAR2(12), OV_LASTUPD_USER VARCHAR2(40), OV_LASTUPD_PROG VARCHAR2(40), OD_LASTUPD 日期, OV_LOCATION VARCHAR2(40), OV_REMARKS VARCHAR2(4000), ON_VERSION NUMBER(10,3), ON_VERSION1 NUMBER(10,3), OV_OBJECT_CLASSIFICATION VARCHAR2(5), OV_OBJECT_REGION VARCHAR2(5), OV_OBJECT_TYPE VARCHAR2(3), ON_VERSION2 号, ON_VERSION3 VARCHAR2(20) ); /
使用 odp.net 自动生成代码
//
------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.3643
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace xxxBIZ
using System;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
using System
.Xml.Serialization;
using System.Xml.Schema;
public class CMMT_OBJECTS_TYPE : INullable, IOracleCustomType, IXmlSerializable
private bool m_IsNull;
private string m_OV_SCOPE;
private string m_OV_REMARKS;
private string m_OV_LASTUPD_USER;
private Decimal m_ON_VERSION1;
private bool m_ON_VERSION1IsNull;
private string m_OV_FIX_REF_NO;
private string m_OV_OBJECT_REGION;
private string m_OV_OBJECT_NAME;
private string m_OV_CATEGORY;
private Decimal m_ON_ORDER;
private bool m_ON_ORDERIsNull;
private System.DateTime m_OD_LASTUPD;
private bool m_OD_LASTUPDIsNull;
private string m_OV_LASTUPD_PROG;
private string m_OV_LOCATION;
private string m_ON_VERSION3;
private Decimal m_ON_VERSION2;
private bool m_ON_VERSION2IsNull;
private Decimal m_ON_VERSION;
private bool m_ON_VERSIONIsNull;
private string m_OV_OBJECT_TYPE;
private string m_OV_TYPE;
private string m_OV_OBJECT_CLASSIFICATION;
public CMMT_OBJECTS_TYPE()
// TODO : Add code to initialise the object
this.m_ON_VERSION1IsNull = true;
this.m_ON_ORDERIsNull = true;
this.m_OD_LASTUPDIsNull = true;
this.m_ON_VERSION2IsNull = true;
this.m_ON_VERSIONIsNull = true;
public CMMT_OBJECTS_TYPE(string str)
// TODO : Add code to initialise the object based on the given string
public virtual bool IsNull
get
return this.m_IsNull;
public static CMMT_OBJECTS_TYPE Null
get
CMMT_OBJECTS_TYPE obj = new CMMT_OBJECTS_TYPE();
obj.m_IsNull = true;
return obj;
[OracleObjectMappingAttribute("OV_SCOPE")]
public string OV_SCOPE
get
return this.m_OV_SCOPE;
set
this.m_OV_SCOPE = value;
[OracleObjectMappingAttribute("OV_REMARKS")]
public string OV_REMARKS
get
return this.m_OV_REMARKS;
set
this.m_OV_REMARKS = value;
[OracleObjectMappingAttribute("OV_LASTUPD_USER")]
public string OV_LASTUPD_USER
get
return this.m_OV_LASTUPD_USER;
set
this.m_OV_LASTUPD_USER = value;
[OracleObjectMappingAttribute("ON_VERSION1")]
public Decimal ON_VERSION1
get
return this.m_ON_VERSION1;
set
this.m_ON_VERSION1 = value;
public bool ON_VERSION1IsNull
get
return this.m_ON_VERSION1IsNull;
set
this.m_ON_VERSION1IsNull = value;
[OracleObjectMappingAttribute("OV_FIX_REF_NO")]
public string OV_FIX_REF_NO
get
return this.m_OV_FIX_REF_NO;
set
this.m_OV_FIX_REF_NO = value;
[OracleObjectMappingAttribute("OV_OBJECT_REGION")]
public string OV_OBJECT_REGION
get
return this.m_OV_OBJECT_REGION;
set
this.m_OV_OBJECT_REGION = value;
[OracleObjectMappingAttribute("OV_OBJECT_NAME")]
public string OV_OBJECT_NAME
get
return this.m_OV_OBJECT_NAME;
set
this.m_OV_OBJECT_NAME = value;
[OracleObjectMappingAttribute("OV_CATEGORY")]
public string OV_CATEGORY
get
return this.m_OV_CATEGORY;
set
this.m_OV_CATEGORY = value;
[OracleObjectMappingAttribute("ON_ORDER")]
public Decimal ON_ORDER
get
return this.m_ON_ORDER;
set
this.m_ON_ORDER = value;
public bool ON_ORDERIsNull
get
return this.m_ON_ORDERIsNull;
set
this.m_ON_ORDERIsNull = value;
[OracleObjectMappingAttribute("OD_LASTUPD")]
public System.DateTime OD_LASTUPD
get
return this.m_OD_LASTUPD;
set
this.m_OD_LASTUPD = value;
public bool OD_LASTUPDIsNull
get
return this.m_OD_LASTUPDIsNull;
set
this.m_OD_LASTUPDIsNull = value;
[OracleObjectMappingAttribute("OV_LASTUPD_PROG")]
public string OV_LASTUPD_PROG
get
return this.m_OV_LASTUPD_PROG;
set
this.m_OV_LASTUPD_PROG = value;
[OracleObjectMappingAttribute("OV_LOCATION")]
public string OV_LOCATION
get
return this.m_OV_LOCATION;
set
this.m_OV_LOCATION = value;
[OracleObjectMappingAttribute("ON_VERSION3")]
public string ON_VERSION3
get
return this.m_ON_VERSION3;
set
this.m_ON_VERSION3 = value;
[OracleObjectMappingAttribute("ON_VERSION2")]
public Decimal ON_VERSION2
get
return this.m_ON_VERSION2;
set
this.m_ON_VERSION2 = value;
public bool ON_VERSION2IsNull
get
return this.m_ON_VERSION2IsNull;
set
this.m_ON_VERSION2IsNull = value;
[OracleObjectMappingAttribute("ON_VERSION")]
public Decimal ON_VERSION
get
return this.m_ON_VERSION;
set
this.m_ON_VERSION = value;
public bool ON_VERSIONIsNull
get
return this.m_ON_VERSIONIsNull;
set
this.m_ON_VERSIONIsNull = value;
[OracleObjectMappingAttribute("OV_OBJECT_TYPE")]
public string OV_OBJECT_TYPE
get
return this.m_OV_OBJECT_TYPE;
set
this.m_OV_OBJECT_TYPE = value;
[OracleObjectMappingAttribute("OV_TYPE")]
public string OV_TYPE
get
return this.m_OV_TYPE;
set
this.m_OV_TYPE = value;
[OracleObjectMappingAttribute("OV_OBJECT_CLASSIFICATION")]
public string OV_OBJECT_CLASSIFICATION
get
return this.m_OV_OBJECT_CLASSIFICATION;
set
this.m_OV_OBJECT_CLASSIFICATION = value;
public virtual void FromCustomObject(Oracle.DataAccess.Client.OracleConnection con, System.IntPtr pUdt)
Oracle.DataAccess.Types.OracleUdt.SetValue(con, pUdt, "OV_SCOPE", this.OV_SCOPE);
Oracle.DataAccess.Types.OracleUdt.SetValue(con, pUdt, "OV_REMARKS", this.OV_REMARKS);
Oracle.DataAccess.Types.OracleUdt.SetValue(con, pUdt, "OV_LASTUPD_USER", this.OV_LASTUPD_USER);
if ((ON_VERSION1IsNull == false))
Oracle.DataAccess.Types.OracleUdt.SetValue(con, pUdt, "ON_VERSION1", this.ON_VERSION1);
Oracle.DataAccess.Types.OracleUdt.SetValue(con, pUdt, "OV_FIX_REF_NO", this.OV_FIX_REF_NO);
Oracle.DataAccess.Types.OracleUdt.SetValue(con, pUdt, "OV_OBJECT_REGION", this.OV_OBJECT_REGION);
Oracle.DataAccess.Types.OracleUdt.SetValue(con, pUdt, "OV_OBJECT_NAME", this.OV_OBJECT_NAME);
Oracle.DataAccess.Types.OracleUdt.SetValue(con, pUdt, "OV_CATEGORY", this.OV_CATEGORY);
if ((ON_ORDERIsNull == false))
Oracle.DataAccess.Types.OracleUdt.SetValue(con, pUdt, "ON_ORDER", this.ON_ORDER);
if ((OD_LASTUPDIsNull == false))
Oracle.DataAccess.Types.OracleUdt.SetValue(con, pUdt, "OD_LASTUPD", this.OD_LASTUPD);
Oracle.DataAccess.Types.OracleUdt.SetValue(con, pUdt, "OV_LASTUPD_PROG", this.OV_LASTUPD_PROG);
Oracle.DataAccess.Types.OracleUdt.SetValue(con, pUdt, "OV_LOCATION", this.OV_LOCATION);
Oracle.DataAccess.Types.OracleUdt.SetValue(con, pUdt, "ON_VERSION3", this.ON_VERSION3);
if ((ON_VERSION2IsNull == false))
Oracle.DataAccess.Types.OracleUdt.SetValue(con, pUdt, "ON_VERSION2", this.ON_VERSION2);
if ((ON_VERSIONIsNull == false))
Oracle.DataAccess.Types.OracleUdt.SetValue(con, pUdt, "ON_VERSION", this.ON_VERSION);
Oracle.DataAccess.Types.OracleUdt.SetValue(con, pUdt, "OV_OBJECT_TYPE", this.OV_OBJECT_TYPE);
Oracle.DataAccess.Types.OracleUdt.SetValue(con, pUdt, "OV_TYPE", this.OV_TYPE);
Oracle.DataAccess.Types.OracleUdt.SetValue(con, pUdt, "OV_OBJECT_CLASSIFICATION", this.OV_OBJECT_CLASSIFICATION);
public virtual void ToCustomObject(Oracle.DataAccess.Client.OracleConnection con, System.IntPtr pUdt)
this.OV_SCOPE = ((string)(Oracle.DataAccess.Types.OracleUdt.GetValue(con, pUdt, "OV_SCOPE")));
this.OV_REMARKS = ((string)(Oracle.DataAccess.Types.OracleUdt.GetValue(con, pUdt, "OV_REMARKS")));
this.OV_LASTUPD_USER = ((string)(Oracle.DataAccess.Types.OracleUdt.GetValue(con, pUdt, "OV_LASTUPD_USER")));
this.ON_VERSION1IsNull = Oracle.DataAccess.Types.OracleUdt.IsDBNull(con, pUdt, "ON_VERSION1");
if ((ON_VERSION1IsNull == false))
this.ON_VERSION1 = ((Decimal)(Oracle.DataAccess.Types.OracleUdt.GetValue(con, pUdt, "ON_VERSION1")));
this.OV_FIX_REF_NO = ((string)(Oracle.DataAccess.Types.OracleUdt.GetValue(con, pUdt, "OV_FIX_REF_NO")));
this.OV_OBJECT_REGION = ((string)(Oracle.DataAccess.Types.OracleUdt.GetValue(con, pUdt, "OV_OBJECT_REGION")));
this.OV_OBJECT_NAME = ((string)(Oracle.DataAccess.Types.OracleUdt.GetValue(con, pUdt, "OV_OBJECT_NAME")));
this.OV_CATEGORY = ((string)(Oracle.DataAccess.Types.OracleUdt.GetValue(con, pUdt, "OV_CATEGORY")));
this.ON_ORDERIsNull = Oracle.DataAccess.Types.OracleUdt.IsDBNull(con, pUdt, "ON_ORDER");
if ((ON_ORDERIsNull == false))
this.ON_ORDER = ((Decimal)(Oracle.DataAccess.Types.OracleUdt.GetValue(con, pUdt, "ON_ORDER")));
this.OD_LASTUPDIsNull = Oracle.DataAccess.Types.OracleUdt.IsDBNull(con, pUdt, "OD_LASTUPD");
if ((OD_LASTUPDIsNull == false))
this.OD_LASTUPD = ((System.DateTime)(Oracle.DataAccess.Types.OracleUdt.GetValue(con, pUdt, "OD_LASTUPD")));
this.OV_LASTUPD_PROG = ((string)(Oracle.DataAccess.Types.OracleUdt.GetValue(con, pUdt, "OV_LASTUPD_PROG")));
this.OV_LOCATION = ((string)(Oracle.DataAccess.Types.OracleUdt.GetValue(con, pUdt, "OV_LOCATION")));
this.ON_VERSION3 = ((string)(Oracle.DataAccess.Types.OracleUdt.GetValue(con, pUdt, "ON_VERSION3")));
this.ON_VERSION2IsNull = Oracle.DataAccess.Types.OracleUdt.IsDBNull(con, pUdt, "ON_VERSION2");
if ((ON_VERSION2IsNull == false))
this.ON_VERSION2 = ((Decimal)(Oracle.DataAccess.Types.OracleUdt.GetValue(con, pUdt, "ON_VERSION2")));
this.ON_VERSIONIsNull = Oracle.DataAccess.Types.OracleUdt.IsDBNull(con, pUdt, "ON_VERSION");
if ((ON_VERSIONIsNull == false))
this.ON_VERSION = ((Decimal)(Oracle.DataAccess.Types.OracleUdt.GetValue(con, pUdt, "ON_VERSION")));
this.OV_OBJECT_TYPE = ((string)(Oracle.DataAccess.Types.OracleUdt.GetValue(con, pUdt, "OV_OBJECT_TYPE")));
this.OV_TYPE = ((string)(Oracle.DataAccess.Types.OracleUdt.GetValue(con, pUdt, "OV_TYPE")));
this.OV_OBJECT_CLASSIFICATION = ((string)(Oracle.DataAccess.Types.OracleUdt.GetValue(con, pUdt, "OV_OBJECT_CLASSIFICATION")));
public virtual void ReadXml(System.Xml.XmlReader reader)
// TODO : Read Serialized Xml Data
public virtual void WriteXml(System.Xml.XmlWriter writer)
// TODO : Serialize object to xml data
public virtual XmlSchema GetSchema()
// TODO : Implement GetSchema
return null;
public override string ToString()
// TODO : Return a string that represents the current object
return "";
public static CMMT_OBJECTS_TYPE Parse(string str)
// TODO : Add code needed to parse the string and get the object represented by the string
return new CMMT_OBJECTS_TYPE();
// Factory to create an object for the above class
[OracleCustomTypeMappingAttribute("PENTACMS.CMMT_OBJECTS_TYPE")]
public class CMMT_OBJECTS_TYPEFactory : IOracleCustomTypeFactory
public virtual IOracleCustomType CreateObject()
CMMT_OBJECTS_TYPE obj = new CMMT_OBJECTS_TYPE();
return obj;
插入代码
private void INSERT_INTO_CMMT_OBJECTS(CMMT_FIX_MASTER_TYPE fixMaster, DateTime dt)
CMMT_OBJECTS_COL cmmt_obj_col = new CMMT_OBJECTS_COL();
CMMT_OBJECTS_COLFactory objList = new CMMT_OBJECTS_COLFactory();
CMMT_OBJECTS_TYPE[] objColl = (CMMT_OBJECTS_TYPE[])objList.CreateArray(DGV_OBJ_LIST.Rows.Count);
for (int i = 0; i < DGV_OBJ_LIST.Rows.Count; i++)
TextBox TextBoxN_ORDER = (TextBox)DGV_OBJ_LIST.Rows[i].Cells[1].FindControl("N_ORDER");
TextBox TextBoxV_OBJECT_NAME = (TextBox)DGV_OBJ_LIST.Rows[i].Cells[2].FindControl("V_OBJECT_NAME");
DropDownList DrpV_TYPE = (DropDownList)DGV_OBJ_LIST.Rows[i].Cells[3].FindControl("V_TYPE");
DropDownList DrpV_LOCATION = (DropDownList)DGV_OBJ_LIST.Rows[i].Cells[4].FindControl("V_LOCATION");
DropDownList DrpV_OBJECT_CLASSIFICATION = (DropDownList)DGV_OBJ_LIST.Rows[i].Cells[5].FindControl("V_OBJECT_CLASSIFICATION");
DropDownList DrpV_OBJECT_REGION = (DropDownList)DGV_OBJ_LIST.Rows[i].Cells[6].FindControl("V_OBJECT_REGION");
TextBox TextBoxV_REMARKS = (TextBox)DGV_OBJ_LIST.Rows[i].Cells[7].FindControl("V_REMARKS");
TextBox TextBoxN_VERSION = (TextBox)DGV_OBJ_LIST.Rows[i].Cells[8].FindControl("N_VERSION");
TextBox TextBoxN_VERSION1 = (TextBox)DGV_OBJ_LIST.Rows[i].Cells[9].FindControl("N_VERSION1");
TextBox TextBoxN_VERSION2 = (TextBox)DGV_OBJ_LIST.Rows[i].Cells[10].FindControl("N_VERSION2");
TextBox TextBoxN_VERSION3 = (TextBox)DGV_OBJ_LIST.Rows[i].Cells[11].FindControl("N_VERSION3");
objColl[i] = new CMMT_OBJECTS_TYPE();
if(TextBoxN_ORDER.Text != "")
objColl[i].ON_ORDER = Convert.ToInt32(TextBoxN_ORDER.Text);
objColl[i].OV_OBJECT_NAME = TextBoxV_OBJECT_NAME.Text;
objColl[i].OV_TYPE = DrpV_TYPE.SelectedValue;
objColl[i].OV_LASTUPD_USER= smsUser.UserID;
objColl[i].OV_LASTUPD_PROG = programName;
objColl[i].OD_LASTUPD = dt;
objColl[i].OD_LASTUPDIsNull = false;
objColl[i].OV_LOCATION = DrpV_LOCATION.SelectedValue;
objColl[i].OV_REMARKS = TextBoxV_REMARKS.Text;
if(TextBoxN_VERSION.Text != "")
objColl[i].ON_VERSION = Convert.ToInt32(TextBoxN_VERSION.Text);
if (TextBoxN_VERSION1.Text != "")
objColl[i].ON_VERSION1 = Convert.ToInt32(TextBoxN_VERSION1.Text);
if (TextBoxN_VERSION2.Text != "")
objColl[i].ON_VERSION2 = Convert.ToInt32(TextBoxN_VERSION2.Text);
objColl[i].ON_VERSION3 = TextBoxN_VERSION3.Text;
objColl[i].OV_OBJECT_CLASSIFICATION = DrpV_OBJECT_CLASSIFICATION.SelectedValue;
objColl[i].OV_OBJECT_REGION = DrpV_OBJECT_REGION.SelectedValue;
cmmt_obj_col.Value = objColl;
fixMaster.OC_CMMT_OBJECTS_COL = cmmt_obj_col;
【问题讨论】:
您是否考虑过仅使用您遇到问题的字段类型制作一个较小的表和 proc?你会更容易缩小你的问题范围,让人们通过一个小代码 sn-p 比你发布的大量代码更合理。 @TomHalladay,感谢汤姆的回复,Juat 检查 int 值,我所有的 int 值都以 ON_ 开头,我知道我所有的 num 变量都没有插入 :( 看起来像一些铸造有问题。 @TomHalladay:我的问题是除了 int column(number) 之外的所有内容都正确插入 【参考方案1】:执行你的参数后我找到了答案
cmd.ExecuteNonQuery();
它导致您的类型类中的 ToCustomObject 函数由 odp.net 在 VS 中生成,并且在该函数中为所有数据和数字您有一个与您的数字或数据参数名称相同的参数作为布尔类型。这个变量以你的列名开始,以 IsNull 结束
OD_SUBMITTEDIsNull
在设置 DATA 或 Number 变量的值后,应将此参数设置为 true。
就是这样:)
OD_SUBMITTEDIsNull = true;
【讨论】:
以上是关于在 odp.net 上使用带有 number 属性的 oracle 对象类型的主要内容,如果未能解决你的问题,请参考以下文章
.NET Core 使用ODP.NET Core连接操作Oracle数据库
将 ODP.Net 与 NHibernate 与 .net System.decimal 一起使用