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

时间:2018-01-05 23:16:40

标签: c# asp.net database oracle

我正在编写一个简单的select / join语句,它只返回我拥有的每条记录的第一行。我不确定这是我的C#中的编码错误,还是我的表的数据库错误。总结一下,我想检索数据库中的所有3条记录。

Oracle Tables:

- Oracle Table Schema

- ldhc_accounts table data

- donors table data

C#代码:

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

        query = "SELECT * FROM ldhc_accounts la JOIN donors d ON la.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();
                d.LName = reader["lname"].ToString();
                d.Address = reader["address"].ToString();
                d.City = reader["city"].ToString();
                d.Province = reader["province"].ToString();
                d.Phone = Convert.ToInt64(reader["phone"]);
                d.LdhcDonorId = Convert.ToInt32(reader["donor_id"]);
                d.TableId = Convert.ToInt32(reader["table_id"]);
                d.DonorCredit = Convert.ToInt64(reader["account_number"]);
                d.DonorCCV = Convert.ToInt32(reader["ccv"]);
                d.DonorExpMonth = Convert.ToInt32(reader["expiry_month"]);
                d.DonorExpYear = Convert.ToInt32(reader["expiry_year"]);
                d.Donor_Amt = Convert.ToInt32(reader["amount"]);
            }

            donors.Add(d);
        }

        conn.Close();

        return donors;
    }

Web表单C#

protected void get_donors_btn_Click(object sender, EventArgs e)
    {
        List<Donor> d = a_oracle.Get();
        foreach(Donor item in d)
        {
            get_donors.InnerHtml += "<li>"
                + item.DonorId + " " 
                + item.FName + " "
                + item.LName + " "
                + item.Address + " "
                + item.City + " "
                + item.Province + " "
                + item.Phone + " "
                + item.LdhcDonorId + " "
                + item.TableId + " "
                + item.DonorCredit + " "
                + item.DonorCCV + " "
                + item.DonorExpMonth + " "
                + item.DonorExpYear + " "
                + item.Donor_Amt + " "
                + "</li>";
        }
    }

HTML代码:

<form id="form1" runat="server">
    <div>
        <h1>Admin Controls</h1>
        <h2>Select Donor Based on ID</h2>
        <div>Donor ID:<asp:TextBox runat="server" ID="get_donor_id"></asp:TextBox></div>
        <div><asp:Button runat="server" ID="get_donor_id_btn" OnClick="get_donor_id_btn_Click" Text="Get Donor" /></div>

        <h2>Select All Donors</h2>
        <div><asp:Button runat="server" ID="get_donors_btn" OnClick="get_donors_btn_Click" Text="Get All Donors" /></div>
        <div>
            <ul runat="server" id="get_donors">

            </ul>
        </div>
        <h2>Select Donors based on Amount</h2>

        <h2>Delete a Donor Record</h2>
        Donor ID:<asp:TextBox runat="server" ID="del_donor_id"></asp:TextBox>
        <div><asp:Button runat="server" ID="del_btn" OnClick="del_btn_Click" Text="Delete"/></div>
        <div><asp:Label runat="server" ID="del_mssg"></asp:Label></div>
        <h2>Update Donor Information</h2>
    </div>
</form>

最终结果: Final Result

提前致谢!

1 个答案:

答案 0 :(得分:0)

我找到了答案。

在我的捐赠者课程中,我的领域是静态的。删除静态,一切按预期工作。