如何从访问数据库中将单个列中的每一行显示为C#窗体中的多个文本框 [英] How to I display each row from a single column into multiple textboxes in C# windows form from access database
本文介绍了如何从访问数据库中将单个列中的每一行显示为C#窗体中的多个文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的作业涉及我创建拼写测试。我需要显示来自单个列的信息,但我希望将该列中的每一行分配给单独的文本框。我似乎无法想象实际做到这一点。那么请你帮忙吗。
我尝试过:
I have an assignment that involves me creating a Spelling Bee Test. I need to display information from a single column but I want each row from that column to be assigned to separate textboxes. I can't seem to figure a away to actually do that. So Can you people help please.
What I have tried:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Spelling_Bee_Retry
{
public partial class Student_Test : Form
{
public Student_Test()
{
InitializeComponent();
}
OleDbCommand cmd = new OleDbCommand();
OleDbConnection con = new OleDbConnection();
OleDbDataAdapter da;
DataSet ds;
DataRow dRow;
int maxRows;
Database_Connection objConnect = new Database_Connection();
private void Student_Test_Load(object sender, EventArgs e)
{
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= Spelling Bee.accdb";//creates a connection with my access database
cmd.Connection = con;
con.Open();
cmd.CommandText = "SELECT * FROM " + globalVariables.testName;//selects the testname according to my tablename from combobox
cmd.ExecuteNonQuery();
OleDbDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
//These are my textboxes. The first textbox displays the information from the first row from the column, but the rest I can't figure it out.
defbx1.Text = dr.GetValue(1).ToString();
defbx2.Text = dr.GetValue(1).ToString();
}
con.Close();
}
推荐答案
如果您使用
cmd.ExecuteReader()
你必须使用查询结果的每一行
you have to use for each row of the query result
reader.Read()
reader.Read()
defbx1.Text = dr.GetValue(1).ToString();
reader.Read()
defbx2.Text = dr.GetValue(1).ToString();
...
这篇关于如何从访问数据库中将单个列中的每一行显示为C#窗体中的多个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文