我是VB的新手。
我关注了视频,但这种情况一直在发生。
这是我的代码
Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
con = New OleDbConnection
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Edward\Documents\AccessDb1.mdb;"
Dim dr As OleDbDataReader
Try
con.Open()
Dim Query As String
Query = "SELECT * FROM tblok where pname='" & ComboBox2.Text & "'"
cmd = New OleDbCommand(Query, con)
dr = cmd.ExecuteReader
While dr.Read
Label2.Text = dr.GetString(dr("pname").ToString)
End While
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
con.Close()
End Try
End Sub
Imports System.Data.OleDb
Public Class Form2
Dim conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Edward\Documents\AccessDb1.mdb;"
Dim con As OleDbConnection = New OleDbConnection(conString)
Dim cmd As OleDbCommand
Dim adapter As OleDbDataAdapter
Dim dt As DataTable = New DataTable()
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
con.Open()
Dim cm As New OleDbCommand("SELECT * FROM tblok WHERE pgen ='Male'", con)
Dim dr As OleDbDataReader = cm.ExecuteReader
While dr.Read
ComboBox2.Items.Add(dr("pname").ToString)
End While
dr.Close()
con.Close()
con.Open()
Dim cf As New OleDbCommand("SELECT * FROM tblok WHERE pgen ='Female'", con)
Dim drf As OleDbDataReader = cf.ExecuteReader
While drf.Read
ComboBox1.Items.Add(drf("pname").ToString)
End While
drf.Close()
con.Close()
End Sub
还有!差点忘了〜
Label2.Text = dr.GetString(+1)
似乎有用..但它只显示名称列
错误就在这里
Label2.Text = dr.GetString(dr("pname").ToString)
我不知道。也许
提前致谢!