Exemplo n.º 1
0
    static void Main(string[] args)
    {
        // Enter user id, password, and Oracle data source (i.e. net service name, EZ Connect, etc.)
        string constr = "user id=<USER ID>;password=<PASSWORD>;data source=<DATA SOURCE>";

        string sqlInsertRef   = "insert into odp_ref2_sample_person_obj_tab values (:1)";
        string sqlSelectRef   = "select ref(p) from odp_ref2_sample_person_obj_tab p";
        string sqlSelectValue = "select value(p) from odp_ref2_sample_person_obj_tab p";
        string udtTypeNameP   = "ODP_REF2_SAMPLE_PERSON_TYPE";
        string objTabNameP    = "ODP_REF2_SAMPLE_PERSON_OBJ_TAB";

        // Create a new Person object.
        Person p = new Person();

        p.Name    = "John";
        p.Address = "Address 1";
        p.Age     = 20;

        // Create a new Student object.
        Student s = new Student();

        s.Name    = "Jim";
        s.Address = "Address 2";
        s.Age     = 25;
        s.Major   = "Physics";

        OracleConnection  con    = null;
        OracleCommand     cmd    = new OracleCommand();
        OracleDataReader  reader = null;
        OracleTransaction txn    = null;
        OracleRef         refP   = null; // person REF
        OracleRef         refS   = null; // student REF

        try
        {
            // Establish a connection to Oracle DB.
            con = new OracleConnection(constr);
            con.Open();
            cmd.Connection = con;

            try
            {
                // Inserting a person and a student instance into the odp_ref2_sample_person_obj_tab
                txn = con.BeginTransaction();

                cmd.CommandText = sqlInsertRef;
                OracleParameter param = new OracleParameter("inParam", OracleDbType.Object, ParameterDirection.Input);
                param.UdtTypeName = udtTypeNameP;
                cmd.Parameters.Add(param);

                // Insert person
                param.Value = p;
                cmd.ExecuteNonQuery();

                // Insert Student
                param.Value = s;
                cmd.ExecuteNonQuery();

                txn.Commit();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception in inserting into {0}: {1}", objTabNameP, ex.Message);
            }
            finally
            {
                if (txn != null)
                {
                    txn.Dispose();
                    txn = null;
                }
                if (cmd != null)
                {
                    cmd.Parameters.Clear();
                }
            }

            try
            {
                // Retrieving REF from odp_ref2_sample_person_obj_tab
                cmd.CommandText = sqlSelectRef;

                reader = cmd.ExecuteReader();
                int row = 1;

                while (reader.Read())
                {
                    if (row == 1)
                    {
                        refP = reader.GetOracleRef(0);
                    }
                    else
                    {
                        refS = reader.GetOracleRef(0);
                    }
                    row++;
                }

                // Fetch rows from database table.
                Person  p1 = (Person)refP.GetCustomObject();
                Student s1 = (Student)refS.GetCustomObject();
                Console.WriteLine("Person: " + p1);
                Console.WriteLine("Student: " + s1);
                Console.WriteLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception in selecting from {0}: {1}", objTabNameP, ex.Message);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Dispose();
                    reader = null;
                }
            }

            try
            {
                // Update person object.
                txn = con.BeginTransaction();

                Person p2 = (Person)refP.GetCustomObject();

                // Update person's age using OracleRef.
                p2.Age = p2.Age + 1;
                refP.Update(p2);

                // p2 is updated to the database.
                txn.Commit();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception in updating person in {0}: {1}", objTabNameP, ex.Message);
            }
            finally
            {
                if (txn != null)
                {
                    txn.Dispose();
                    txn = null;
                }
            }

            // Delete student object.
            try
            {
                txn = con.BeginTransaction();
                refS.Delete();
                txn.Commit();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception in deleting student from {0}: {1}", objTabNameP, ex.Message);
            }
            finally
            {
                if (txn != null)
                {
                    txn.Dispose();
                    txn = null;
                }
            }

            try
            {
                // Retrieve rows from the database table.
                cmd.CommandText = sqlSelectValue;
                cmd.CommandType = CommandType.Text;
                reader          = cmd.ExecuteReader();

                // Fetch each row.
                int rowCount = 1;
                while (reader.Read())
                {
                    // Fetch the objects as a custom type.
                    Person p3;
                    if (reader.IsDBNull(0))
                    {
                        p3 = Person.Null;
                    }
                    else
                    {
                        p3 = (Person)reader.GetValue(0);
                    }

                    Console.WriteLine("Row {0}: {1}", rowCount++, p3);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception in selecting from {0}: {1}", objTabNameP, ex.Message);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Dispose();
                    reader = null;
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Get exception: " + ex.Message);
        }
        finally
        {
            // Clean up
            if (refS != null)
            {
                refS.Dispose();
            }
            if (refP != null)
            {
                refP.Dispose();
            }
            if (reader != null)
            {
                reader.Dispose();
            }
            if (cmd != null)
            {
                cmd.Dispose();
            }
            if (txn != null)
            {
                txn.Dispose();
            }
            if (con != null)
            {
                con.Close();
                con.Dispose();
            }
        }
    }
Exemplo n.º 2
0
        internal IList <RubricaEntitaType> GetContattiOrgByName(string nomeEntita)
        {
            List <RubricaEntitaType> listEntities = null;

            using (OracleCommand oCmd = base.CurrentConnection.CreateCommand())
            {
                oCmd.CommandText = "SELECT VALUE(v0)"
                                   + " FROM v_rubr_entita_obj v0"
                                   + " WHERE :p_id_org IN (upper(v0.ragione_sociale), upper(v0.ufficio))";
                oCmd.Parameters.Add("p_id_org", nomeEntita.ToUpper());

                try
                {
                    using (OracleDataReader r = oCmd.ExecuteReader())
                    {
                        if (r.HasRows)
                        {
                            listEntities = new List <RubricaEntitaType>();

                            while (r.Read())
                            {
                                RubricaEntitaType ent = (RubricaEntitaType)(r.GetOracleValue(0));
                                if (!ent.RUBR_CONTATTI_REFS.IsNull)
                                {
                                    ent.Contatti = new List <RubricaContatti>();
                                    foreach (string o in ent.RUBR_CONTATTI_REFS.ListContattiRef)
                                    {
                                        OracleRef           or = new OracleRef(base.CurrentConnection, o);
                                        RubricaContattiType rc = (RubricaContattiType)or.GetCustomObject(OracleUdtFetchOption.Server);
                                        rc.SetEntita(ent);
                                        ent.Contatti.Add(rc);
                                    }
                                }
                                listEntities.Add(ent);
                            }
                        }
                    }
                }
                catch
                {
                    listEntities = null;
                }
            }

            return(listEntities);
        }