我有一个表试试 - “uid、pwd、mob 和名称。”
CL001 abc 9876589 admin
CL002 def 8909898 admin2
我想从表中以视觉基本形式调用值并在相应的文本框中显示值。我知道这个 SQL 查询 -->select * from try where uid="CL001"
这是一个允许您在数据库上执行 SELECT 语句的函数。在调用它之前,您必须声明一个新的数据表,例如:
Dim dt as New DataTable
dt = doSQL("select * from try where uid='CL001'")
Public Function DoSQL(ByVal sqlCmd As String) As DataTable
Dim cmd As New SQL.SqlCommand()
Dim cnn As New SQL.SqlConnection(My.Settings.cnnString) ' your connection string here
Dim sqlDT As New DataTable
With cmd
.Connection = cnn
.CommandText = sqlCmd
.CommandType = CommandType.Text
End With
Dim da As New SQL.SqlDataAdapter()
da.SelectCommand = cmd
da.Fill(sqlDT)
Return sqlDT
End Function
数据表将具有匹配的记录。因此,如果您知道只有 1 条记录,您可以执行以下操作:
textbox.text = dt.Rows(0).Item("uid").ToString
或者你可以将它绑定到一个文本框并使用 bindingmanagerbase 来跟踪你喜欢的位置:
Private WithEvents bm As BindingManagerBase ' Class scope
Private Sub partBind(ByVal dt As DataTable)
txtBox.DataBindings.Add("Text", dt, "uid")
End Sub
然后,您可以使用 bm.position 控制翻转记录,如下所示:
Private Sub btnNavClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click, btnPrevious.Click, btnLast.Click, btnFirst.Click
Dim btnClicked As Button = DirectCast(sender, Button)
Select Case btnClicked.Name
Case "btnNext"
If bm.Position < bm.Count - 1 Then
bm.Position += 1
Else
bm.Position = 0
End If
Case "btnPrevious"
If bm.Position > 0 Then
bm.Position -= 1
Else
bm.Position = bm.Count
End If
bm.Position -= 0
Case "btnLast"
bm.Position = bm.Count
Case "btnFirst"
bm.Position = 0
End Select
End Sub