'【功能】:【显示ListView中的内容】
'【输入】:【连接字符串,要显示字符串,是否显示check框,是哪种访问数据库的方式】
'【输出】:【数据库的查询内容在ListView中显示】
'【参数】:【showStr,传值调用】
Private Sub showListView(ByVal strConn As String, ByVal showStr As String, ByVal check As Boolean, ByVal dataAccess As String)
If dataAccess = "sql" Then
'[用于sql查询]
Dim sqlConn As SqlClient.SqlConnection
Dim sqlReader As SqlClient.SqlDataReader
Dim sqlCmd As SqlClient.SqlCommand
Try
sqlConn = New SqlClient.SqlConnection(strConn)
sqlConn.Open()
sqlCmd = New SqlClient.SqlCommand(showStr, sqlConn)
sqlReader = sqlCmd.ExecuteReader
'【加入listview的头标题】
Dim iHead As Integer
ListView1.Columns.Clear()
For iHead = 0 To sqlReader.FieldCount - 1
ListView1.Columns.Add(sqlReader.GetName(iHead), 100, HorizontalAlignment.Center)
Next
'[加入每一列数据]
ListView1.Items.Clear()
Dim iRow As Long = 0
Dim iRowHead As Int16
While sqlReader.Read
ListView1.Items.Add(sqlReader(0))
For iRowHead = 1 To sqlReader.FieldCount - 1
If (sqlReader(irowhead) Is System.DBNull.value = False) Then
ListView1.Items(iRow).SubItems.Add(CStr(sqlReader(iRowHead)))
Else
ListView1.Items(irow).SubItems.Add(" ")
End If
Next
iRow = iRow + 1
End While
Catch
MessageBox.Show(Err.Description, clewInfo, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
sqlConn.Close()
Exit Sub
End Try
sqlReader.Close()
sqlCmd.Dispose()
sqlConn.Close()
Else
'[用于ole查询]
Dim oleConn As OleDb.OleDbConnection
Dim oleReader As OleDb.OleDbDataReader
Dim oleCmd As OleDb.OleDbCommand
Try
oleConn = New OleDb.OleDbConnection(strConn)
oleConn.Open()
oleCmd = New OleDb.OleDbCommand(showStr, oleConn)
oleReader = oleCmd.ExecuteReader
'【加入listview的头标题】
Dim iHead As Integer
ListView1.Columns.Clear()
For iHead = 0 To oleReader.FieldCount - 1
ListView1.Columns.Add(oleReader.GetName(iHead), 100, HorizontalAlignment.Center)
Next
'[加入每一列数据]
ListView1.Items.Clear()
Dim iRow As Long = 0
Dim iRowHead As Int16
While oleReader.Read()
ListView1.Items.Add(oleReader(0))
For iRowHead = 1 To oleReader.FieldCount - 1
If oleReader(irowhead) Is System.DBNull.value = False Then
ListView1.Items(iRow).SubItems.Add(CStr(oleReader(iRowHead)))
Else
ListView1.Items(irow).SubItems.Add(" ")
End If
Next
iRow = iRow + 1
End While
Catch
MessageBox.Show(Err.Description, clewInfo, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
oleConn.Close()
Exit Sub
End Try
oleReader.Close()
oleCmd.Dispose()
oleConn.Close()
End If
'【加入底色】
Dim i As Integer
For i = 0 To ListView1.Items.Count - 1
If i Mod 2 = 0 Then
ListView1.Items(i).BackColor = Color.LightBlue
Else
ListView1.Items(i).BackColor = Color.OldLace
End If
Next i
'[是否显示check按钮]
If check = True Then
ListView1.CheckBoxes = True
Else
ListView1.CheckBoxes = False
End If
End Sub
本文地址:http://com.8s8s.com/it/it44263.htm