簡體   English   中英

如何從vb .net中的ms訪問表中獲取下一條記錄

[英]how to fetch next record from ms access table in vb .net

我正在使用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

但我無法使用此代碼獲取下一行..pls幫我..謝謝

此代碼在哪里運行? 通過按鈕單擊? 上面的代碼似乎只顯示最后一行,然后拋出異常,因為在我變得比行數大之后索引將超出范圍。

i整數需要在更大的范圍內聲明,並且每次移動到下一行時都要確保,在繼續操作之前,請確保不超出行數的范圍。

例如,如果您通過單擊按鈕來執行此操作,則將:

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

由於ds已填充,並且i在更廣泛的范圍內聲明,因此可以在此子目錄中訪問它,您需要做的只是檢查以確保剩余行,如果有,請顯示該行。 一旦顯示出來,就為下一行增加i,以便當您再次單擊該按鈕時,它具有下一行索引的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM