2016-11-18 29 views
0

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

嗨,我试图代码,我想从一个名为FruitsDatabase.accdb一个Microsoft Access 2016数据库名为Fruits_Table表检索值C#应用程序。然后我想在我的表单中的标签中显示这些值,分别称为label1,label2,label3。

仅供参考,我在名为“Fruits.cs”的另一个类文件中使用了名为Fruit(构造函数,字符串名称,字符串颜色)的构造函数。然后我将这个构造函数传入我的PresentationGUI表单。

我的数据库

数据库有一个名为Fruits_Table一个表,该表中有两列:“名称”为水果的名称和“颜色”的颜色。数据库中记录了三项:苹果 - 红色,香蕉 - 黄色,梨 - 绿色。

我设法得到工作至今

我设法让我的数据库连接成功。所以没有必要为此担心。从数据库检索值也很好。

我的问题

我的问题是这样的:

只有在我的数据库中的第一个条目似乎显示,无论我点击其中的水果图片框。换句话说,无论我点击picBox1,picBox2还是picBox3,它都会显示'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) {....} 
    } 

    } 
}  

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

回答

5

您正在加载相同的值到您的阵列三次,所以请尝试更改虽然声明是这样的:

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; 
    } 
} 

我建议你第一次进入您的查询返回的行数和然后根据该值初始化您的数组。

+0

不错!这工作...有点。除了现在我的行​​似乎是颠倒的:它显示梨,第一个香蕉是绿色,第二个是黄色,最后是苹果,红色时应该是相反的。我应该将计数器设置为3并将其减1吗? – 5120bee

+1

如何在您的Select语句中添加订单,例如sql =“SELECT * FROM Fruits_Table ORDER BY ...”; –

相关问题