把sql字符串中选择的内容显示在ListView

 '【功能】:【显示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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值