我正在尝试在 vb.net 中创建一个下一步按钮来浏览我的访问数据库
I am trying to create a next button in vb.net to navigate through my access database
问题是无论我从哪个位置开始,每次按下一步都是从第一条记录开始,然后正常向前移动,而我要从我所在的位置开始。
我真的是编程新手,已经为此苦苦挣扎了 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
问题是无论我从哪个位置开始,每次按下一步都是从第一条记录开始,然后正常向前移动,而我要从我所在的位置开始。 我真的是编程新手,已经为此苦苦挣扎了 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