Vb.net连接数据库,字母列显示在Listbox1里,点击项目,TextBox1就显示数字列相对

2024-11-15 23:47:34
推荐回答(1个)
回答1:

其实很简单,但是写起来还是比较费力
Imports System.Data.OleDb
Public Class Form2
Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\project\office\test.accdb")
Sub bindtolist()
Dim cmd As New OleDbCommand("select b from maintab", conn)
Dim adp As New OleDbDataAdapter(cmd)
conn.Open()
Dim tb As New DataTable
adp.Fill(tb)
ListBox1.DataSource = tb
ListBox1.DisplayMember = "b"
conn.Close()
End Sub
Sub bindtotext(ByVal str As String)
Dim cmd As New OleDbCommand("SELECT a FROM MainTab WHERE (b = @s)", conn)
Dim adp As New OleDbDataAdapter(cmd)
cmd.Parameters.Add(New OleDbParameter("@s", OleDb.OleDbType.Char)).Value = str
conn.Open()
Dim td As New DataTable
adp.Fill(td)
TextBox1.Text = td.Rows(0).Item("a")
conn.Close()
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
bindtolist()
End Sub

Private Sub ListBox1_Click(sender As Object, e As EventArgs) Handles ListBox1.Click
Dim str As String = ListBox1.Text
bindtotext(str)
End Sub
End Class