vb中如何将sql2000中的表内容导入到listbox

我想将SQL2000中的数据库中的一个表的内容,通过查询语句,完整地导入到一个listbox中,并也要有表的那个格式(几行几列的)
最新回答
碎雨带风

2024-10-02 15:23:08

'VB.net的,你看一下,VB6的也有,先这个吧
Public Function GetListView(ByRef lvListView As ListView, ByVal SQLString As String) As Boolean

Using da As New SqlDataAdapter(SQLString, cnn)
Dim dt As New DataTable
Try
da.Fill(dt)
Catch ex As Exception
Return False
End Try

Dim c As Integer
Dim cs As Integer()

ReDim cs(0)
With lvListView
.Items.Clear()
'建立结构

Dim lch As ColumnHeader
'Dim lg As ListViewGroup

'.Columns.Clear()

For c = 0 To dt.Columns.Count - 1
lch = .Columns.Add(dt.Columns(c).Caption)
ReDim Preserve cs(c)

Next

If .Columns.Count < dt.Columns.Count Then

For c = 0 To .Columns.Count - 1

.Columns(c).Text = dt.Columns(c).Caption
ReDim Preserve cs(c)
Next

For c = .Columns.Count To dt.Columns.Count - 1
lch = .Columns.Add(dt.Columns(c).Caption)
ReDim Preserve cs(c)
Next
Else
For c = 0 To dt.Columns.Count - 1
.Columns(c).Text = dt.Columns(c).Caption
ReDim Preserve cs(c)
Next

Do Until .Columns.Count = dt.Columns.Count
.Columns.RemoveAt(.Columns.Count - 1)
Loop

End If

Dim Row As DataRow
Dim d As Double
Dim s As String

'Item

Dim Item As ListViewItem

For Each Row In dt.Rows

'Item = .Items.Add(Row.Item(cs(0)).ToString)
Item = New ListViewItem
Item.Text = Row.Item(cs(0)) & ""

For c = 1 To dt.Columns.Count - 1
s = Row(c).ToString
If IsNumeric(s) Then
Try
d = Convert.ToDouble(s)
Item.SubItems.Add(d)
Catch ex As Exception
Item.SubItems.Add(s)
End Try
Else
Item.SubItems.Add(s)
End If

Next
.Items.Add(Item)

Next

End With

dt.Dispose()
da.Dispose()

End Using
End Function
长双眼皮的鱼

2024-10-02 16:53:45

上面的这些和其他的类似
......
While dr.Read
listbox.Items.Add(dr(0).ToString)
End While
......

注:dr 是 SqlDataReader