'连接数据库
Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\test.accdb")
'打开数据库
conn.Open()
'生成命令
Dim cmd As New OleDbCommand("SELECT ID,stuname FROM studenttbl", conn)
'执行命令
Dim reader As OleDbDataReader '声明OleDbDataReader对象
reader = cmd.ExecuteReader() '实例OleDbDataReader对象,注意不能用New
Dim Strreader As String = ""
While reader.Read() '读取数据
For i = 0 To reader.FieldCount - 1
If reader.IsDBNull(i) = False Then
Strreader &= reader(i).ToString & " "
End If
Next
Strreader &= Environment.NewLine
End While
If Not reader.IsClosed Then
reader.Close() '关闭OleDbDataReader对象
End If
'关闭数据库
conn.Close()