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

王朝mssql·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

竟δ堋浚骸鞠允綥istView中的内容】

【输入】:【连接字符串,要显示字符串,是否显示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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航