Vb.net连接数据库,字母列显示在Listbox1里,点击项目,TextBox1就显示数字列相对
Vb.net连接数据库,字母列显示在Listbox1里,点击项目,TextBox1就显示数字列相对应的行内容?求完整代码,新手学习...
Vb.net连接数据库,字母列显示在Listbox1里,点击项目,TextBox1就显示数字列相对应的行内容?求完整代码,新手学习
展开
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
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
追问
Dim cmd As New OleDbCommand("SELECT a FROM MainTab WHERE (b = @s)", conn)
那个@s是什么?什么作用
追答
这个标识是自己写的,没有特殊规定,只要是@开头的,系统就把它作为参数的占位,然后运用绑定获得程序参数
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询