帮帮我们..我想使用Visual Studio 2010(vb.net)中的List视图在MS访问中显示我的表的记录..
我的程序步骤:
在文本框中输入表名
然后单击按钮以在MS Access中搜索
如果找到了表名,则在Listview中显示记录
(抱歉英语不好)
如何使用List View vb.net在MS Access中显示表上的记录?
这是我的代码:::
Imports System.Data.OleDb
Public Class Search
Dim shcon As New OleDbConnection
Dim shqry As String = Nothing
Dim shcmd As New OleDbCommand
Dim shdr As OleDbDataReader
Dim shdel As String = Nothing
Dim shstr As String = Nothing
'Co-connect to Database
#Region "Connection"
Sub ConnToDB()
Try
With shcon
If .State = ConnectionState.Open Then .Close()
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=database\masterlist.accdb"
.Open()
End With
Catch ex As Exception
MessageBox.Show("Unable to connect", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Application.Exit()
End Try
End Sub
#End Region
' Add Columns sa Listview
#Region "LVSettings"
Sub LVsettings()
With SLV.Columns
.Add("Date", 50, HorizontalAlignment.Left)
.Add("[AM]Time-in", 100, HorizontalAlignment.Left)
.Add("[AM]Time-out", 100, HorizontalAlignment.Left)
.Add("[PM]Time-in", 100, HorizontalAlignment.Left)
.Add("[PM]Time-out", 100, HorizontalAlignment.Left)
End With
End Sub
#End Region
'display the data from database
#Region "FillListView"
Sub FillListview()
SLV.Items.Clear()
shqry = "SELECT * from " & txtSID.text & " ORDER BY id ASC"
shcmd = New OleDbCommand(shqry, shcon)
shdr = shcmd.ExecuteReader
While shdr.Read
With SLV
.Items.Add(shdr("DATE"))
With .Items(.Items.Count - 1)
.SubItems.Add(shdr("AM-TIME-IN"))
.SubItems.Add(shdr("AM-TIME-OUT"))
.SubItems.Add(shdr("PM-TIME-IN"))
.SubItems.Add(shdr("PM-TIME-OUT"))
End With
End With
End While
End Sub
#End Region
Private Sub btnsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsearch.Click
Call FillListview()
End Sub
Private Sub LV1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SLV.SelectedIndexChanged
End Sub
Private Sub Search_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call LVsettings()
Call ConnToDB()
End Sub
End Class
答案 0 :(得分:0)
无论如何等待。我设法找到方法.. 这是更新:
之前:
While shdr.Read
With SLV
.Items.Add(shdr("DATE"))
With .Items(.Items.Count - 1)
.SubItems.Add(shdr("AM-TIME-IN"))
.SubItems.Add(shdr("AM-TIME-OUT"))
.SubItems.Add(shdr("PM-TIME-IN"))
.SubItems.Add(shdr("PM-TIME-OUT"))
End With
End While
后:
While shdr.Read
With SLV
.Items.Add(shdr("DATE"))
With .Items(.Items.Count - 1)
If Not IsDBNull(shdr("AM-TIME-IN")) Then .SubItems.Add(shdr("AM-TIME-IN"))
If Not IsDBNull(shdr("AM-TIME-OUT")) Then .SubItems.Add(shdr("AM-TIME-OUT"))
If Not IsDBNull(shdr("PM-TIME-IN")) Then .SubItems.Add(shdr("PM-TIME-IN"))
If Not IsDBNull(shdr("PM-TIME-OUT")) Then .SubItems.Add(shdr("PM-TIME-OUT"))
End With
End With
End While
感谢您的时间。
这是我的完整代码:
Imports System.Data.OleDb
Public Class Search
Dim shcon As New OleDbConnection
Dim shqry As String = Nothing
Dim shcmd As New OleDbCommand
Dim shdr As OleDbDataReader
Dim shdel As String = Nothing
Dim shstr As String = Nothing
'Co-connect to Database
#Region "Connection"
Sub ConnToDB()
Try
With shcon
If .State = ConnectionState.Open Then .Close()
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=database\masterlist.accdb"
.Open()
End With
Catch ex As Exception
MessageBox.Show("Unable to connect", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Application.Exit()
End Try
End Sub
#End Region
' Add Columns sa Listview
#Region "LVSettings"
Sub LVsettings()
With SLV.Columns
.Add("Date", 50, HorizontalAlignment.Left)
.Add("[AM]Time-in", 100, HorizontalAlignment.Left)
.Add("[AM]Time-out", 100, HorizontalAlignment.Left)
.Add("[PM]Time-in", 100, HorizontalAlignment.Left)
.Add("[PM]Time-out", 100, HorizontalAlignment.Left)
End With
End Sub
#End Region
'display the data from database
#Region "FillListView"
Sub FillListview()
SLV.Items.Clear()
shqry = "SELECT * from " & txtSID.text & " ORDER BY id ASC"
shcmd = New OleDbCommand(shqry, shcon)
shdr = shcmd.ExecuteReader
While shdr.Read
With SLV
.Items.Add(shdr("DATE"))
With .Items(.Items.Count - 1)
If Not IsDBNull(shdr("AM-TIME-IN")) Then .SubItems.Add(shdr("AM-TIME-IN"))
If Not IsDBNull(shdr("AM-TIME-OUT")) Then .SubItems.Add(shdr("AM-TIME-OUT"))
If Not IsDBNull(shdr("PM-TIME-IN")) Then .SubItems.Add(shdr("PM-TIME-IN"))
If Not IsDBNull(shdr("PM-TIME-OUT")) Then .SubItems.Add(shdr("PM-TIME-OUT"))
End With
End With
End While
End Sub
#End Region
Private Sub btnsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsearch.Click
Call FillListview()
End Sub
Private Sub LV1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SLV.SelectedIndexChanged
End Sub
Private Sub Search_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call LVsettings()
Call ConnToDB()
End Sub
End Class