Requirements:
- start the SQL Server or ensure the SQL Server has running.
- add reference to Microsoft ActiveX Data Objects 2.0 Library (ADODB).
- add Microsoft Windows Common Controls 6.0 component (ListView).
Private Sub Form_Load()
'recordset and connection variables
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim lstItem As ListItem
'open the connection
Set cn = New ADODB.Connection
cn.Open "Provider=SQLOLEDB.1;User ID=sa;Password=password;" _
+ "Location=LOCAL;Database=pubs"
'open the recordset
Set rs = New Recordset
rs.Open "authors", cn, adOpenForwardOnly, adLockReadOnly
Do Until rs.EOF
Set lstItem = ListView1.ListItems.Add(, rs("au_id"), _
rs("au_lname") & ", " & rs("au_fname"))
lstItem.SubItems(1) = rs("address")
lstItem.SubItems(2) = rs("city")
lstItem.SubItems(3) = rs("state")
lstItem.SubItems(4) = rs("zip")
rs.MoveNext
Loop
'close and clean up
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub
to add reference, click Add References In Visual Basic Classic >
Tidak ada komentar:
Posting Komentar