将不同的数据插入组合框列表 [英] Insert distinct data into Combobox List

查看:20
本文介绍了将不同的数据插入组合框列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想问一下如何将我的不同数据插入到我的组合框中?

I just want to ask if how to insert my distinct data to my combobox?

dim query as string
query = "SELECT DISTINCT Department FROM tblEmployees" 

combobox1.text = ????

谢谢!

推荐答案

为了将 Access 数据库中的数据导入 VB.NET 程序,您可以使用 OleDbConnection, OleDbCommandOleDbDataReader 类.

In order to get the data from your Access database into a VB.NET program, you can use instances of the OleDbConnection, OleDbCommand and OleDbDataReader classes.

您需要编写一个连接字符串.该链接还提供了有关为其他版本的 Access 构建连接字符串的详细信息.

You will need to compose a connection string. That link also provides details about constructing connection strings for other versions of Access.

您的 SQL 语句应如下所示:

Your SQL statement should look something like this:

SELECT DISTINCT FieldName
FROM TableName

使用 Do.. 遍历 OleDbDataReader循环语句.

Iterate through the OleDbDataReader using the Do..Loop statement.

项目被添加到 WinForms 组合框 使用 ComboboxItems.Add 方法.您可以在 Do..Loop 中使用此方法添加项目.

Items are added to a WinForms combobox using the Combobox's Items.Add method. You can use this method within the Do..Loop to add the items.

您可能会考虑的另一个选项,特别是如果您想在将数据放入组合框之前使用 .NET 代码处理数据(可以使用 SQL 完成的处理通常在 SQL 中性能更高),是将每个项目放入一些集合,然后您可以使用 Items.AddRange 方法一次添加多个项目.

Another option you might consider, especially if you want to process the data using .NET code before putting it into the combo box (processing that can be done with SQL is usually more performant in SQL), is to put each item into some collection and then you can use Items.AddRange method to add multiple items at once.

这篇关于将不同的数据插入组合框列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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