注册
 找回密码
 注册
江西广告网
查看: 270|回复: 0
打印 上一主题 下一主题

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

[复制链接]

该用户从未签到

1
跳转到指定楼层
发表于 2009-1-18 17:16:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?注册

x
  【功能】:【显示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             <
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表