C#中的Oracle SELECT语句只返回第一行多次 [英] Oracle SELECT statement in C# returning only first row multiple times

查看:249
本文介绍了C#中的Oracle SELECT语句只返回第一行多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

- 我有两个表的简单select语句(带连接)。

-虽然读者正在读这个,但我正在创建一个新实例,然后调用我的类,其中包含来自每个表列的信息。

- 然后,我在列表的末尾添加此实例。



但经过数周的调试后,我意识到由于某种原因,在这种情况下,没有创建一个新的'd'实例。因此,每次从类中检索信息时,它同时替换列表中的前一个数组。这导致每次产生相同的行。这是我的代码。



  public  List< Donor>获取()
{
// 定义字段
列表<供体GT; donor = new List< Donor>();
string 查询;
OracleCommand cmd;
OracleDataReader阅读器;

query = SELECT * FROM ldhc_accounts l JOIN donor d ON l.donor_id = d。 ID;
conn.Open();
cmd = new OracleCommand(query,conn);
reader = cmd.ExecuteReader();

while (reader.Read())
{
Donor d = new Donor();
{
d.DonorId = Convert.ToInt32(reader [ id ]);
d.FName = reader [ fname]。ToString();
donor.Add(d);
}

}

conn.Close();

返回捐赠者;
}







C#文件:



  protected   void  get_donors_btn_Click( object  sender,EventArgs e)
{
List< Donor> donor = a_oracle.Get();
foreach (捐赠者项目捐赠者中)
{
get_donors。 InnerHtml + = < li>
+ item.DonorId +
+ item.FName +
+ < / li> ;
}
}





捐赠者班级



 命名空间 FinalProj.Models 
{
public class 捐助者:ldhc
{
// 字段
private static Int64 _donor_credit_card;
private static int _donor_ccv ;
private static int _donor_expiry_month ;
private static int _donor_expiry_year ;
private static int _donor_amt ;
private static int _donor_rows ;
私有 静态 对象 _donor_id ;

// 方法
public int Donor_Amt
{
get {< span class =code-keyword> return _donor_amt; }
set {_donor_amt = value ; }
}

public Int64 DonorCredit
{
get { return _donor_credit_card; }
set {_donor_credit_card = value ; }
}

public int DonorCCV
{
get { return _donor_ccv; }
set {_donor_ccv = value ; }
}

public int DonorExpMonth
{
get { return _donor_expiry_month; }
set {_donor_expiry_month = value ; }
}

public int DonorExpYear
{
get { return _donor_expiry_year; }
set {_donor_expiry_year = value ; }
}

public int DRUS
{
get { return _donor_rows; }
set {_donor_rows = value ; }
}

public object DonorId
{
get { return _donor_id; }
set {_donor_id = value ; }
}

// public ldhc Ldhc {get;组; }
}
}





我的尝试:



-我试图重新安排我的新实例的创建位置,我已经检查了所有其他代码,并且终于意识到这里有一些东西是错误的。



- 如果我创建一个select语句并包含一个WHERE子句,它可以完美地工作并且只返回我需要的一行。



- 我已经删除了一些我正在尝试检索的其他列,以使这段代码更清晰,但是当我在SQL开发人员中运行这个select语句时,它完全可以返回3不同的行。



-我很难过。堆栈溢出是难倒的(或者问题太长了)。



-让我知道你是否还需要其他任何信息。

解决方案

在你的 Donor 类,您已将所有字段声明为 static



这意味着这些字段只有一个值,无论你创建了多少个类的实例。



删除来自字段的静态修饰符,您的代码应该有效。

  public   class 捐助者:ldhc 
{
// 字段
private Int64 _donor_credit_card;
private int _donor_ccv;
private int _donor_expiry_month;
private int _donor_expiry_year;
private int _donor_amt;
private int _donor_rows;
私人 对象 _donor_id;



static(C#参考)| Microsoft Docs [ ^ ]


无法访问您的数据,我们无法做很多事情。

您可以尝试的东西 - 而不是DataReader ,使用DataAdapter,并填充DataTable。

然后,您可以使用调试器确切地查看返回的行数以及它们包含的确切内容。 Donor构造函数不太可能每次都返回相同的实例 - 这会严重影响对象构造函数的工作方式 - 因此返回的数据更有可能在每一行上显示相同而不是重新使用相同的对象实例

-I have a simple select statement (with a join) of two tables.
-While the reader is reading this, I'm creating a new instance, and then calling my classes that will contain the information from each of my table columns.
-Then, I'm adding this instance at the end of the list.

BUT after weeks of debugging, I've realized that for some reason, a new instance of 'd' in this case, isn't being created. So, each time it goes through retrieving the information from the classes, it simultaneously replaces the previous array in the list. This results in the same row being produced each time. Here is my code.

public List<Donor> Get()
        {
            //Define fields
            List<Donor> donors = new List<Donor>();
            string query;
            OracleCommand cmd;
            OracleDataReader reader;

            query = "SELECT * FROM ldhc_accounts l JOIN donors d ON l.donor_id = d.id";
            conn.Open();
            cmd = new OracleCommand(query, conn);
            reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                Donor d = new Donor();
                {
                    d.DonorId = Convert.ToInt32(reader["id"]);
                    d.FName = reader["fname"].ToString();
                    donors.Add(d);
                }

            }

            conn.Close();

            return donors;
        }




C# File:

protected void get_donors_btn_Click(object sender, EventArgs e)
        {
            List<Donor> donors = a_oracle.Get();
            foreach(Donor item in donors)
            {
                get_donors.InnerHtml += "<li>"
                    + item.DonorId + " " 
                    + item.FName + " "
                    + "</li>";
            }
        }



Donors Class

namespace FinalProj.Models
{
    public class Donor : ldhc
    {
        //Fields
        private static Int64 _donor_credit_card;
        private static int _donor_ccv;
        private static int _donor_expiry_month;
        private static int _donor_expiry_year;
        private static int _donor_amt;
        private static int _donor_rows;
        private static object _donor_id;

        //Methods
        public int Donor_Amt
        {
            get { return _donor_amt; }
            set { _donor_amt = value; }
        }

        public Int64 DonorCredit
        {
            get { return _donor_credit_card; }
            set { _donor_credit_card = value; }
        }

        public int DonorCCV
        {
            get { return _donor_ccv; }
            set { _donor_ccv = value; }
        }

        public int DonorExpMonth
        {
            get { return _donor_expiry_month; }
            set { _donor_expiry_month = value; }
        }
        
        public int DonorExpYear
        {
            get { return _donor_expiry_year; }
            set { _donor_expiry_year = value; }
        }

        public int DRows
        {
            get { return _donor_rows; }
            set { _donor_rows = value; }
        }

        public object DonorId
        {
            get { return _donor_id; }
            set { _donor_id = value; }
        }

        //public ldhc Ldhc { get; set; }
    }
}



What I have tried:

-I've tried to rearrange where my new instance is created, I've triple checked all my other code, and have finally realized that there is something here that is wrong.

-If I create a select statement and include a WHERE clause, it works perfectly and returns only a single row that I need.

-I've removed some other columns I'm trying to retrieve to make this code a little cleaner, but when I run this select statement in SQL developer, it works perfectly to return 3 distinct rows.

-I'm stumped. Stack Overflow is stumped (or it's too long winded of a question).

-Let me know if there's any other information you need.

解决方案

In your Donor class, you've declared all of the fields as static.

That means there's only ever a single value for those fields, no matter how many instances of your class you create.

Remove the static modifier from the fields, and your code should work.

public class Donor : ldhc
{
    //Fields
    private Int64 _donor_credit_card;
    private int _donor_ccv;
    private int _donor_expiry_month;
    private int _donor_expiry_year;
    private int _donor_amt;
    private int _donor_rows;
    private object _donor_id;


static (C# Reference) | Microsoft Docs[^]


Without access to your data, there isn't a lot we can do.
Something you can try - instead of a DataReader, use a DataAdapter, and fill a DataTable.
You can then use the debugger to see exactly how many rows you are getting returned and exactly what they contain. It's unlikely that the Donor constructor is returning the same instance each time - that would take some serious bending of the way object constructors work - so it's much more likely that the data returned appears identical on each row rather that re-using the same object instance.


这篇关于C#中的Oracle SELECT语句只返回第一行多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆