当您读取查询的结果集时,您会使用循环。
While dr.Read
' run this for every row in your resultset
...
End While
The While
循环继续进行,直到读完所有行。
您不必使用循环。如果您希望一次读取一行,就像这样
If dr.Read
' just the first row
End If
If dr.Read
' just the second row
End If
If dr.Read
' just the third row
End If
...
从你的问题我猜你有Textbox1
, Textbox2
, ... Textbox5
在您的表格上。我也猜你有Grade1
, Grade2
....
要同时处理科目名称和成绩,请将查询的第一行更改为
sql = "SELECT subject_name, grade " & _
您可以像这样填充这些项目:
If dr.Read
TextBox1.Text = dr.Item("subject_name").ToString
Grade1.Text = dr.Item("grade").ToString
End If
If dr.Read
TextBox2.Text = dr.Item("subject_name").ToString
Grade2.Text = dr.Item("grade").ToString
End If
If dr.Read
TextBox3.Text = dr.Item("subject_name").ToString
Grade3.Text = dr.Item("grade").ToString
End If
' more of these sets of four lines to fill your whole form.
这解决了你的问题。但您可能会注意到它重复得荒谬。您真正需要的是一个文本框数组(实际上是两个数组)。您在程序中创建并填充这些文本框。我还没有调试过这个:那是你要做的。
Dim Subjects As Textbox()
Dim Grades As Textbox()
...
Dim rownumber, Y
rownumber = 0
Y = 200
Dim Subject
Dim Grade
While dr.Read
Subject = New Textbox
Subject.Text = dr.Item("subject_name").ToString
Subject.Width = 200
Subject.Height = 40
Subject.X = 175
Subject.Y = Y
Subjects(rownumber) = Subject
Form.Controls.Add(Subject)
Grade = New Textbox
Grade.Text = dr.Item("grade").ToString
Grade.Width = 50
Grade.Height = 40
Grade.X = 400
Grade.Y = Y
Grades(rownumber) = Grade
Form.Controls.Add(Grade)
rownumber = rownumber + 1
Y = Y + 50
End While
运行时,您将拥有两列控件,每个控件对应一列。但是这段代码很复杂,您必须使用以下命令来完成表单的所有布局Something.Y = value
进而Y = Y + 50
算术。
这就是网格控件存在的原因。他们负责处理这类事情。