简体   繁体   English

oracle数据库的C#问题

[英]C# problem with oracle database

Hello I want to fill a list with data, that I get from an Oracle database. 您好,我想用从Oracle数据库获得的数据填充列表。 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. 我们确实需要该错误消息以提供更好的帮助,但是我认为我将研究数据读取器中的读取项。

your code 您的代码

while (oradr.Read())
   {
      Console.WriteLine(oradr.GetInt32(0) + " \t " + oradr.GetString(1));
   }

I would replace with 我将替换为

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])) 然后将Filialen.Add(oradr(0))转换为Filialen.Add(Convert.ToString(oradr[0]))

You should stop using System.Data.OracleClient classes, as they're deprecated . 您应该停止使用System.Data.OracleClient类,因为它们已被弃用 Use ODP.NET instead. 请改用ODP.NET

As of your problem, try replacing oradr.GetInt32 with oradr.GetDecimal . 解决您的问题时,请尝试将oradr.GetInt32替换为oradr.GetDecimal Other thing you should note, is that Filialen.Add(oradr(0)) will not even compile, since oradr is not a method. 您应该注意的另一件事是,由于oradr不是方法,因此Filialen.Add(oradr(0))甚至都不会编译。 You probably missed to add the method's name. 您可能错过了添加方法名称的步骤。

If this does not work, please provide the full exception information. 如果这不起作用,请提供完整的异常信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM