Hello I want to fill a list with data, that I get from an Oracle database. But it keeps giving me errors.
This is my connection class
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows.Forms;
namespace Kunst_In_Huis_C
{
class Connectie
{
String strApplicationName = Application.ProductName;
Logging objLoggingApplication = new Logging("d:\\project.log");
private OracleConnection con;
public Connectie()
{
con = new OracleConnection();
}
public void openConnectie()
{
if (con.State == ConnectionState.Closed)
{
con = new OracleConnection("Data Source=192.168.1.106/orcl;User ID=???;Password=???;Unicode=True");
con.Open();
}
}
public void sluitConnectie()
{
try
{
this.con.Close();
}
catch (Exception)
{
Console.WriteLine("fout bij het afsluiten van de connectie...");
}
}
public void voerQueryUit(String sql)
{
try
{
OracleCommand cmd = new OracleCommand(sql, con);
OracleDataReader oradr = cmd.ExecuteReader();
while (oradr.Read())
{
Console.WriteLine(oradr.GetInt32(0) + " \t " + oradr.GetString(1));
}
oradr.Close();
}
catch (OracleException ex)
{
Console.WriteLine("Oracle Error\n" + ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("General App Error" + ex.Message);
}
finally
{
con.Close();
}
}
public void VulFilialen()
{
try
{
OracleCommand cmd = new OracleCommand("Select Fil_Adres from Filialen", con);
OracleDataReader oradr = cmd.ExecuteReader();
List<String> Filialen = new List<String>();
oradr = cmd.ExecuteReader();
while (oradr.Read())
{
Filialen.Add(oradr(0));
}
return Filialen;
}
catch (Exception ex)
{
objLoggingApplication.WriteLine(strApplicationName, ex.Message);
}
finally
{
con.Close();
}
}
}
}
It keeps giving me errors on my return and also when i want to add something to the list.
We really need the error message to provide better help, but I think that I would look into the reading items from the datareader.
while (oradr.Read())
{
Console.WriteLine(oradr.GetInt32(0) + " \t " + oradr.GetString(1));
}
while (oradr.Read())
{
Console.WriteLine(Convert.ToString(oradr[0]) + " \t "
+ Convert.ToString(oradr[1]));
}
I could be way off here, but looking at the code, this is the parts that jump out at me.
and then convert Filialen.Add(oradr(0))
to Filialen.Add(Convert.ToString(oradr[0]))
You should stop using System.Data.OracleClient classes, as they're deprecated . Use ODP.NET instead.
As of your problem, try replacing oradr.GetInt32
with oradr.GetDecimal
. Other thing you should note, is that Filialen.Add(oradr(0))
will not even compile, since oradr
is not a method. You probably missed to add the method's name.
If this does not work, please provide the full exception information.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.