为什么我的C#代码只显示数据库中的第一行?

时间:2016-11-18 02:08:05

标签: c# arrays database forms constructor

我的目标

嗨,我正在尝试编写一个C#应用程序,我希望在名为FruitsDatabase.accdb的Microsoft Access 2016数据库中从名为Fruits_Table的表中检索值。然后,我想在标签中以名称label1,label2,label3。

显示这些值

仅供参考,我在名为" Fruits.cs"的另一个类文件中使用名为Fruit(string Name,string Color)的构造函数。然后我将这个构造函数传递给我的PresentationGUI表单。

我的数据库

数据库有一个名为Fruits_Table的表,在该表中有两列:" Name"为了水果的名称和"颜色"因为它的颜色。数据库中记录了三个条目:Apple - 红色,香蕉色 - 黄色,梨色 - 绿色。

到目前为止我管理的工作

我设法与我的数据库建立了一个SUCCESSFUL连接。因此,没有必要担心这一点。从数据库中检索值也很有用。

我的问题

我遇到的问题是:

无论我点击哪个水果图片框,我的数据库中的第一个条目似乎都在显示。换句话说,无论我点击picBox1,picBox2还是picBox3,它都只能显示Apple' Apple'和' Red'

如何迭代遍历数据库的行?

using System;
...
using system.Windows.Forms;
using system.Data.OleDb;

namespace project1
{
     public partial class PresentationGUI : Form
     {
          private OleDbConnection aConn;
          private OleDbCommand aCmd;
          private OleDbDataReader aReader;

          private string aConnection;
          private string sql;

          Fruit[] aFruit = new Fruit[10];

     public PresentationGUI()
     {
          Initialize Component;

          //This is working fine.
          aConnection = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = FruitsDatabase.accdb;";

          sql = "SELECT * FROM Fruits_Table";
          aCmd = new OleDbCommand();
          aCmd.CommandText = sql;

          aCmd.Connection = aConn;
          aReader = aCmd.ExecuteReader();

          while (aReader.Read())
          {

             //I have a Fruit constructor that goes:  Fruit(string Name, string Color) in another class named Fruit.cs, and I use it here for this form.
             aFruit[0] = new Fruit(aReader["Name"].ToString(), aReader["Color"].ToString());
             aFruit[1] = new Fruit(aReader["Name"].ToString(), aReader["Color"].ToString());
             aFruit[2] = new Fruit(aReader["Name"].ToString(), aReader["Color"].ToString());
          }

     private void PresentationGUI_Load(object sender, EventArgs e) {...}

     //Intended Output:  when the user clicks on picBox1 (showing an image of an apple) it should display 'Apple' for label1 and 'red' for label2
    //Reality:  it displays 'Apple' for label1 and 'Red' for label2
     private void picBox1_Click(object sender, EventArgs e)
     {
         try
         {
               this.label1.Text = aFruit[0].Name.ToString();
               this.label2.Text = aFruit[0].Color.ToString();
         }
         catch (NullReferenceException) {....}
     }

     //Intended Output:  when the user clicks on picBox2 (showing an image of an Banana) it should display 'Banana' for label1 and 'yellow' for label2
    //Reality:  it displays 'Apple' for label1 and 'Red' for label2
     private void picBox2_Click(object sender, EventArgs e)
     {
         try
         {
               this.label1.Text = aFruit[1].Name.ToString();
               this.label2.Text = aFruit[1].Color.ToString();
         }
         catch (NullReferenceException) {....}
     }

     //Intended Output:  when the user clicks on picBox2 (showing an image of an Pear) it should display 'Pear' for label1 and 'green' for label2
    //Reality:  it displays 'Apple' for label1 and 'Red' for label2
    private void picBox3_Click(object sender, EventArgs e)
     {
         try
         {
               this.label1.Text = aFruit[2].Name.ToString();
               this.label2.Text = aFruit[2].Color.ToString();
         }
         catch (NullReferenceException) {....}
     }

     }
}    

为什么它只显示我数据库的第一行?

1 个答案:

答案 0 :(得分:5)

您正在将相同的值加载到数组中三次,因此请尝试更改您的While语句:

int fruitCounter = 0
while (aReader.Read())
{
    //I have a Fruit constructor that goes:  Fruit(string Name, string Color) in another class named Fruit.cs, and I use it here for this form.
    aFruit[fruitCounter] = new Fruit(aReader["Name"].ToString(), aReader["Color"].ToString());
    fruitCounter++;

    if(fruitCounter > aFruit.Length)
    {
          break;
    }
}

我建议您首先获取查询返回的行数,然后根据该值初始化数组。