我正在尝试在 vb.net 中创建下一个按钮以浏览我的访问数据库



问题是,无论我从哪个位置开始,每次我按下一步时,它都会从第一条记录开始,然后正确地向前移动,而我希望它从我所在的位置开始。 我真的是编程新手,并且已经为此苦苦挣扎了 2 天:( 这是该表单的代码

Imports System.Data.OleDb
Imports System.IO
Public Class Details
Private Sub ShowData(CurrentRow)
Try
TbxSName.Text = Dst.Tables("Mushrooms").Rows(CurrentRow)("Scientific_Name")
TbxCName.Text = Dst.Tables("Mushrooms").Rows(CurrentRow)("Common_Name")
TbxDescription.Text = Dst.Tables("Mushrooms").Rows(CurrentRow)("Description")
TbxEdibilty.Text = Dst.Tables("Mushrooms").Rows(CurrentRow)("Edibility")
TbxLocation.Text = Dst.Tables("Mushrooms").Rows(CurrentRow)("Location")
TbxMorphology.Text = Dst.Tables("Mushrooms").Rows(CurrentRow)("Morphology")
Catch ex As Exception
MsgBox(ex.Message, "error")
End Try
End Sub
Sub clr()
TbxSName.Clear()
TbxCName.Clear()
TbxDescription.Clear()
TbxEdibilty.Clear()
TbxLocation.Clear()
TbxMorphology.Clear()
End Sub
Private Sub Details_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call connection()
Currentrow = 0
Dad = New OleDbDataAdapter("SELECT * FROM Mushrooms", cn)
Dad.Fill(Dst, "Mushrooms")
ShowData(Currentrow)

End Sub
Private Sub BtnNext_Click(sender As Object, e As EventArgs) Handles BtnNext.Click
Call connection()
If Currentrow = Dst.Tables("Mushrooms").Rows.Count - 1 Then
MsgBox("Last Record is Reached", MsgBoxStyle.Exclamation)
Else
Currentrow += 1
ShowData(Currentrow)
End If
cn.Close()
End Sub

End Class

不要从某个地方调用连接。在使用它的地方创建它。需要关闭和处置连接。即使出现错误,使用块也可以为您完成此操作。您似乎不需要您提供的代码中的 DataAdapter 或 DataSet。只需使用数据表和命令即可。

由于您尚未分享声明 Currentrow 的位置或方式,因此我无法告诉您为什么它没有更新。此外,您尚未共享ShowData方法,因此我无法判断那里是否出了问题。

Private conString As String = "Your connection string"
Private CurrentRow As Integer
Private dt As DataTable
Private Sub Details_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dt = New DataTable
Using cn As New OleDbConnection(conString),
cmd As New OleDbCommand("SELECT * FROM Mushrooms", cn)
dt.Load(cmd.ExecuteReader)
End Using
CurrentRow = 0
ShowData()
End Sub
Private Sub BtnNext_Click(sender As Object, e As EventArgs) Handles BtnNext.Click
If CurrentRow >= dt.Rows.Count - 1 Then
MessageBox.Show("Last Record is Reached")
Else
CurrentRow += 1
ShowData()
End If
End Sub
Private Sub ShowData()
TbxSName.Text = dt.Rows(CurrentRow)("Scientific_Name").ToString
TbxCName.Text = dt.Rows(CurrentRow)("Common_Name").ToString
TbxDescription.Text = dt.Rows(CurrentRow)("Description").ToString
TbxEdibilty.Text = dt.Rows(CurrentRow)("Edibility").ToString
TbxLocation.Text = dt.Rows(CurrentRow)("Location").ToString
TbxMorphology.Text = dt.Rows(CurrentRow)("Morphology").ToString
End Sub

相关内容

  • 没有找到相关文章

最新更新