[英]how to fetch next record from ms access table in vb .net
i am working with a MCQ app in vb .net i want to fetch every record frrom MS ACCESS database table called Q_A....here is my code 我正在使用vb .net中的MCQ应用程序,我想从名为Q_A的MS ACCESS数据库表中获取每条记录。这是我的代码
Dim i As Integer = 0
con = New OleDb.OleDbConnection
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=MCQ.accdb"
Try
Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM Q_A ", con)
If ds.Tables("Q_A").Rows.Count > 0 Then
da.Fill(ds, "Q_A")
lblQno.Text = ds.Tables("Q_A").Rows(i).Item(0).ToString()
lblQuestion.Text = ds.Tables("Q_A").Rows(i).Item(1).ToString()
rbtn1.Text = ds.Tables("Q_A").Rows(i).Item(2).ToString()
rbtn2.Text = ds.Tables("Q_A").Rows(i).Item(3).ToString()
rbtn3.Text = ds.Tables("Q_A").Rows(i).Item(4).ToString()
rbtn4.Text = ds.Tables("Q_A").Rows(i).Item(5).ToString()
i = i + 1
End If
con.Close()
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
but i am not able to fetch next row using this code..pls help me..thanks in advance 但我无法使用此代码获取下一行..pls帮我..谢谢
Where is this code being run? 此代码在哪里运行? From a button Click?
通过按钮单击? The code above would appear to only display the last row and then throw an exception as the index would go out of bounds after i becomes larger than the rowcount.
上面的代码似乎只显示最后一行,然后抛出异常,因为在我变得比行数大之后索引将超出范围。
The i integer would need to be declared in a wider scope and with each movement to the next row, ensure that you are not out of bounds with the rowcount before proceeding. i整数需要在更大的范围内声明,并且每次移动到下一行时都要确保,在继续操作之前,请确保不超出行数的范围。
For example if you were doing this from the click of a button you would: 例如,如果您通过单击按钮来执行此操作,则将:
Dim i as integer = 0
Do your database query and populate your dataset
Inside your Button Click Event
if i < rowcount then
lblQno.Text = ds.Tables("Q_A").Rows(i).Item(0).ToString()
lblQuestion.Text = ds.Tables("Q_A").Rows(i).Item(1).ToString()
rbtn1.Text = ds.Tables("Q_A").Rows(i).Item(2).ToString()
rbtn2.Text = ds.Tables("Q_A").Rows(i).Item(3).ToString()
rbtn3.Text = ds.Tables("Q_A").Rows(i).Item(4).ToString()
rbtn4.Text = ds.Tables("Q_A").Rows(i).Item(5).ToString()
i = i + 1
end if
Since the ds is populated and i is declared in a wider scope it is accessible in this sub and all you need to do is check to ensure there are rows left and if so, display that row. 由于ds已填充,并且i在更广泛的范围内声明,因此可以在此子目录中访问它,您需要做的只是检查以确保剩余行,如果有,请显示该行。 Once it's displayed increment i for the next row so that when you click the button again, it has the value of the next row index.
一旦显示出来,就为下一行增加i,以便当您再次单击该按钮时,它具有下一行索引的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.