且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#中的Oracle SELECT语句只返回第一行多次

更新时间:2023-01-21 18:18:58

在你的 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.