首页 > 解决方案 > vb.net as I query Access database and display it in listview?

问题描述

I'm creating a form where I can do the following: please see the image As you can see, I have a txt_id_up and txt_id_dw in the database I want to make the following query.

SELECT * FROM Tabla1
WHERE ID BETWEEN 3 AND 7;

where txt_id_up = 3, and txt_id_dw = 7;

    Dim connection As OleDbConnection
    Dim command As OleDbCommand
    Dim data_reader As OleDbDataReader

    '------------------------------
    'connect to ms.access database
    connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data 
    Source= data\base.accdb;Persist Security Info=False")
    connection.Open()
    'reading data from Tabla1 table
    command = New OleDbCommand("SELECT * FROM Tabla1", connection)
    data_reader = command.ExecuteReader
    '----------------------------------
    'here the code to show in listview1 is missing
    '----------------------------------

and in passing I would like to ask another question, can only the following columns be shown in listview? Name Account

I clarify that I use the datagridview to see it in general and the listview for queries

标签: databasevb.netlistview

解决方案


我不知道我是否收到您的问题,但如果您想从数据库中显示名称帐户,我建议您使用DataGridView.

在表单中添加一个DataGridView控件并添加以下代码:

Dim connection As OleDbConnection
Dim command As OleDbCommand
Dim data_adapter As OleDbDataAdapter

'------------------------------
'connect to ms.access database
connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data 
Source= data\base.accdb;Persist Security Info=False")
connection.Open()
'reading data from Tabla1 table
command = New OleDbCommand("SELECT Name, Account FROM Tabla1 WHERE ID BETWEEN 3 AND 7", connection)
data_adapter = New OleDbDataAdapter(command)

'add results to DataGridView1
 Dim datatable as New DataTable("Table")
 data_adapter.Fill(datatable)          
 DataGridView1.DataSource = datatable  

推荐阅读