vb.net 将 MS Access 数据库中的列值填充到 VB .Net Combo-Box 下拉值中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25036151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:55:33  来源:igfitidea点击:

Populate column values from MS Access database into VB .Net Combo-Box dropdown values?

vb.netcombobox

提问by DK2014

I am accessing data from Access MDB file using my application developed in VB .net 2010. I want to get values from a particular table-column to be populated in a Combo Box dropdown.

我正在使用我在 VB .net 2010 中开发的应用程序访问 Access MDB 文件中的数据。我想从要填充到组合框下拉列表中的特定表列中获取值。

I followed a similar thread @ C# Adding data to combo-box from MS Access tablebut as the code is in C#, I was not able to successfully implement it. And reverted back to my previous code.

我遵循了一个类似的线程@ C#从 MS Access 表向组合框添加数据,但由于代码是在 C# 中,我无法成功实现它。并恢复到我以前的代码。

Because the Table will be updated by another application as well, every time I use my utility I need to get the latest data from that table. Hence I would like to have a feature that could get all available id from table.

因为该表也将被另一个应用程序更新,所以每次我使用我的实用程序时,我都需要从该表中获取最新数据。因此,我想要一个可以从表中获取所有可用 id 的功能。

Below is what I have been trying to do..

以下是我一直在尝试做的..

'below declared at class level. 
' Dim cnnOLEDB As New OleDbConnection
' Dim cmdOLEDB As New OleDbCommand

cnnOLEDB.Open()

cmdOLEDB.CommandText = "select unique_id from tag_data"
cmdOLEDB.Connection = cnnOLEDB

Dim rdrOLEDB2 As OleDbDataReader = cmdOLEDB.ExecuteReader

If rdrOLEDB2.Read = True Then
    TagComboBox1.DataSource = rdrOLEDB2.Item(0).ToString

    TagComboBox1.ValueMember = "unique_id"
    TagComboBox1.DisplayMember = "unique_id"

End If
rdrOLEDB2.Dispose()
rdrOLEDB2.Close()
cnnOLEDB.Close()

采纳答案by ???ěxě?

I would recommend filling a DataTablewith your query. Then you can set the DataSourceof the combobox, ValueMember & the DisplayMember.

我建议填写DataTable您的查询。然后你可以设置DataSource组合框的值,ValueMember 和 DisplayMember。

 Dim dt As New DataTable
 Dim query As String = "select unique_id from tag_data"

 Using connection As New OleDbConnection("your connection string")
  Using command As New OleDbCommand(query, connection)
    Using adapter As New OleDbDataAdapter(command)
        connection.Open()
        adapter.Fill(dt)
        connection.Close()
    End Using
  End Using
 End Using

 If dt.Rows.Count > 0 Then
  TagComboBox1.DataSource = dt
  TagComboBox1.ValueMember = "unique_id"
  TagComboBox1.DisplayMember = "unique_id"
 End If