C# 为什么我的C代码只显示数据库的第一行?
我的目标 您好,我正在尝试编写一个C应用程序,其中我希望从Microsoft Access 2016名为FruitsDatabase.accdb的数据库中名为Fruits_table的表中检索值。然后,我想以label1、label2、label3的形式在标签中显示这些值 仅供参考,我在另一个名为Fruits.cs的类文件中使用了名为Fruitstring Name、string Color的构造函数。然后我将这个构造函数传递到我的PresentationUI表单中 我的数据库 数据库有一个名为FROUTS\ U table的表,该表中有两列:水果名称的名称和颜色的颜色。数据库中记录了三个条目:苹果红、香蕉黄、梨绿 到目前为止,我所做的工作 我成功地连接到了我的数据库。所以没必要担心。从数据库中检索值也很好 我的问题 我的问题是: 无论我点击哪个水果图片框,似乎都只显示数据库中的第一个条目。换句话说,无论我点击picBox1、picBox2或picBox3,它都只会显示“苹果”和“红色” 如何遍历数据库中的行C# 为什么我的C代码只显示数据库的第一行?,c#,arrays,database,forms,constructor,C#,Arrays,Database,Forms,Constructor,我的目标 您好,我正在尝试编写一个C应用程序,其中我希望从Microsoft Access 2016名为FruitsDatabase.accdb的数据库中名为Fruits_table的表中检索值。然后,我想以label1、label2、label3的形式在标签中显示这些值 仅供参考,我在另一个名为Fruits.cs的类文件中使用了名为Fruitstring Name、string Color的构造函数。然后我将这个构造函数传递到我的PresentationUI表单中 我的数据库 数据库有一个名为
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) {....}
}
}
}
为什么它只显示“我的数据库”的第一行?您正在将相同的值加载到数组中三次,因此请尝试如下更改您的While语句:
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) {....}
}
}
}
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;
}
}
我建议您首先获取查询返回的行数,然后根据该值初始化数组。您要将相同的值加载到数组中三次,因此请尝试如下更改您的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;
}
}
我建议您首先获取查询返回的行数,然后根据该值初始化数组。Nice!那起作用了。。。有点。但现在我的行似乎颠倒过来了:它显示的是梨,第一行是绿色,然后是香蕉,第二行是黄色,最后一行是苹果,反之则是红色。我应该将计数器设置为3并将其减量吗?在Select语句中添加一个Order by如何;美好的那起作用了。。。有点。但现在我的行似乎颠倒过来了:它显示的是梨,第一行是绿色,然后是香蕉,第二行是黄色,最后一行是苹果,反之则是红色。我应该将计数器设置为3并将其减量吗?在Select语句中添加一个Order by如何;