c# select query to Oracle database throws “Custom

2019-07-31 16:17发布

问题:

I am trying to map a simple Oracle UDT in my c# application. When I try to extract data from DataReader, it throws the following exception:

Custom type mapping for 'dataSource='DB' schemaName='C##LAZAR' typeName='MATICNIBROJ_T'' is not specified or is invalid

Oracle UDT is:

create or replace 
TYPE            "MATICNIBROJ_T" AS OBJECT (
  MaticniBroj NUMBER(13)
)
INSTANTIABLE NOT FINAL

Custom class used for mapping:

[OracleCustomTypeMapping("C##LAZAR.MATICNIBROJ_T")]
public class MaticniBrojT : IOracleCustomType
    {
        [OracleObjectMappingAttribute("MaticniBroj")]
        public virtual int MaticniBroj { get; set; }

        //c# custom type --> Oracle UDT
        public virtual void FromCustomObject(OracleConnection conn, IntPtr obj)
        {
            OracleUdt.SetValue(conn, obj, "MaticniBroj", this.MaticniBroj);
        }

        //Oracle UDT --> c# custom type
        public virtual void ToCustomObject(OracleConnection conn, IntPtr obj)
        {
            this.MaticniBroj = ((int)(OracleUdt.GetValue(conn, obj, "MaticniBroj")));
        }
    }

DB communication:

    string upit = @"SELECT PIB, NAZIV, RACUN, FAX, TELEFON, ADRESA, EMAIL,
    MATICNIBR FROM KOMITENT_VW k WHERE UPPER(NAZIV) LIKE '%" + naziv.ToUpper() + "%'";

                    conn = napraviKonekciju();

                    conn.Open();

                OracleCommand cmd = new OracleCommand();

                cmd.Connection = conn;

                cmd.CommandText = upit;
                cmd.CommandType = CommandType.Text;

                OracleDataReader dr = cmd.ExecuteReader();

                DataSet ds = new DataSet();
                DataTable dt = new DataTable();

                dt.Columns.Add("PIB");
                dt.Columns.Add("NAZIV");
                dt.Columns.Add("RACUN");
                dt.Columns.Add("FAX");
                dt.Columns.Add("TELEFON");
                dt.Columns.Add("ADRESA");
                dt.Columns.Add("EMAIL");
                dt.Columns.Add("MATICNIBR");
                ds.Tables.Add(dt);

                while(dr.Read())
                {
                    DataRow red = dt.NewRow();
                    red["PIB"] = dr.GetInt32(0);
                    red["NAZIV"] = dr.GetString(1);
                    red["RACUN"] = dr.GetString(2);
                    red["FAX"] = dr.GetString(3);
                    red["TELEFON"] = dr.GetString(4);
                    red["ADRESA"] = dr.GetString(5);
                    red["EMAIL"] = dr.GetString(6);
                    //----next line throws an exception
                    MaticniBrojT mBroj = (MaticniBrojT)dr.GetValue(7);

                    red["MATICNIBR"] = Convert.ToInt64(mBroj.MaticniBroj);
                }

                return ds;
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                conn.Close();
                conn.Dispose();
                conn = null;
            }

Most of the code is provided by user LSA. The errors are mine, though. Can someone clarify where I went wrong?

回答1:

In your error message it says Custom type mapping for 'dataSource='DB' schemaName='C##USER' typeName='MATICNIBROJ_T'' is not specified or is invalid

And in your custom type mapping it says [OracleCustomTypeMapping("C##LAZAR.MATICNIBROJ_T")]

Try adjusting it and run the code again.