将列值从MS Access数据库填充到VB .Net组合框下拉值? [英] Populate column values from MS Access database into VB .Net Combo-Box dropdown values?

查看:557
本文介绍了将列值从MS Access数据库填充到VB .Net组合框下拉值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我跟着一个类似的线程@ C#从MS Access表中将数据添加到组合框中但是因为代码在C#中,我无法成功实现它。

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

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

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.

下面是我一直在努力做的。

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()


推荐答案

我建议用您的查询填充 DataTable 。然后,您可以设置组合框的值 DataSource ,ValueMember&

I would recommend filling a DataTable with your query. Then you can set the DataSource of the combobox, ValueMember & the 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

这篇关于将列值从MS Access数据库填充到VB .Net组合框下拉值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆