我正在创建一个表单,可以执行以下操作: please see the image 如您所见,我有一个 txt_id_up 和 txt_id_dw 在数据库中,我要进行以下查询。
SELECT * FROM Tabla1
WHERE ID BETWEEN 3 AND 7;
其中 txt_id_up = 3,而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
'----------------------------------
顺便说一句,我想问另一个问题, listview 中只能显示以下几列吗? 名称 帐户
我澄清,我通常使用 datagridview 进行查看,并使用 listview进行查询
答案 0 :(得分:0)
我不知道是否收到您的问题,但是如果您想从数据库中显示姓名和帐户,建议您使用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
答案 1 :(得分:0)
我可能会将两个文本框向后退。
Public Class Form3
Private Sub FillListView()
ListView1.BeginUpdate() 'keeps the control from repainting on each addition
Using connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data
Source= data\base.accdb;Persist Security Info=False")
Dim command As New OleDbCommand("SELECT ID, Name FROM Tabla1 Where ID Between ? And ?;", connection)
command.Parameters.Add("FirstID", OleDbType.Integer).Value = CInt(txt_id_up.Text)
command.Parameters.Add("SecondID", OleDbType.Integer).Value = CInt(txt_id_dw.Text)
connection.Open() 'Open the connection at the last possible minute
Using data_reader = command.ExecuteReader
While data_reader.Read()
Dim li As New ListViewItem()
li.Text = CStr(data_reader.GetValue(0)) 'ID
li.SubItems.Add(CStr(data_reader.GetValue(1))) 'Name
ListView1.Items.Add(li)
Loop
End Using
End Using
ListView1.EndUpdate()
End Sub
End Class
编辑
希望这会有所帮助。