locked
Access to Oracle Database using C# .NET RRS feed

  • General discussion

  • Im following an example to access a table form Oracle while implementing Crystal Report in Visual Studio 2013

    The code will not allow me to open a connection..... this is the code

     string oradb = "User Id=user;Password=password;Data Source=database";
            

            OracleCommand cmd = new OracleCommand();
            cmd.CommandText = query;

            using (OracleConnection con = new OracleConnection(oradb))
            {
                PSXLATITEM dsPSXLATITEM = new PSXLATITEM();
                cmd.Connection = con;
                con.Open(); --------> This is where it errors out....
                using (OracleDataReader sdr = cmd.ExecuteReader())
                {
                    //Get the List of all TextObjects in Section2.
                    List<TextObject> textObjects = crystalReport.ReportDefinition.Sections["Section2"].ReportObjects.OfType<TextObject>().ToList();
                    for (int i = 0; i < textObjects.Count; i++)
                    {
                        //Set the name of Column in TextObject.
                        textObjects[i].Text = string.Empty;
                        if (sdr.FieldCount > i)
                        {
                            textObjects[i].Text = sdr.GetName(i);
                        }
                    }
                    //Load all the data rows in the Dataset.
                    while (sdr.Read())
                    {
                        DataRow dr = dsPSXLATITEM.Tables[0].Rows.Add();
                        for (int i = 0; i < sdr.FieldCount; i++)
                        {
                            dr[i] = sdr[i];
                        }
                    }
                }
                con.Close();
                return dsPSXLATITEM;
            }

          while the below code in a console application I dont get an error when trying to open the connection..

            string constr = "User Id=user;Password=password;Data Source=database";
            OracleConnection con = new OracleConnection(constr);
            con.Open();
     
            // Execute a SQL SELECT
            OracleCommand cmd = con.CreateCommand();
            cmd.CommandText = "select * from emp";
            OracleDataReader reader = cmd.ExecuteReader();
     
            // Print all employee numbers
            while (reader.Read())
                Console.WriteLine(reader.GetInt32(0));
     
            // Clean up
            reader.Dispose();
            cmd.Dispose();
            con.Dispose();


    michael john ocasio


    • Edited by mjocasio23 Tuesday, December 4, 2018 9:41 PM
    • Changed type mjocasio23 Wednesday, December 5, 2018 7:23 AM
    Tuesday, December 4, 2018 9:40 PM

All replies