我想用数据库中的2列填充列表框
我的列表框中只显示一列
我想从我的数据库中显示FistName和LastName,但只显示FistName。
以下是我的代码:
Try
connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Nikko Jaze Fabellon\Documents\ASRASIM.accdb")
connection.Open()
ds = New DataSet
tables = ds.Tables
dataAdapter = New OleDbDataAdapter("SELECT [FirstName],[LastName] from [Personnel] where [Status] = 'Activated' ", connection)
dataAdapter.Fill(ds, "Personnel")
Dim view1 As New DataView(tables(0))
With personnelList
.DataSource = ds.Tables("Personnel")
.DisplayMember = "FirstName"
.ValueMember = "ID"
.SelectedIndex = 0
End With
connection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
答案 0 :(得分:1)
只需强制您的查询返回以这种方式连接在一起的两个字段(并且不要忘记ValueMember中使用的字段ID)
Dim cmdText = "SELECT ID, [FirstName] & ' ' & [LastName] as FullName
FROM [Personnel]
WHERE [Status] = 'Activated'"
dataAdapter = New OleDbDataAdapter(cmdText, connection)
dataAdapter.Fill(ds, "Personnel")
Dim view1 As New DataView(tables(0))
With personnelList
.DataSource = ds.Tables("Personnel")
.DisplayMember = "FullName"
.ValueMember = "ID"
.SelectedIndex = 0
End With
请注意,此语法对MS-Access有效。我不确定您是否可以使用相同的语法来连接其他数据库系统中的两个字段。