如何在 datagridview 中显示数据从 access 数据库
datagridview是vb控件,在datagridview中显示数据从 access 数据库步骤如下:
1 在vb中创建窗体:
插入datagrid:
2 编写查询数据的代码:
Private Sub Command1_Click()
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sqlStr As String
Dim i As Integer
Dim contStr As String
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\test.mdb;Persist Security Info=False"
cn.Open
sqlStr = "select 题号,题目内容,答案 from Tradition where 题目内容 like '%" & Text1.Text & "%" & Chr(39)
rs.CursorLocation = adUseClient
rs.Open sqlStr, cn, adOpenDynamic, adLockPessimistic
If rs.RecordCount > 0 Then
Set DataGrid1.DataSource = rs
Else
MsgBox "查询无记录"
End If
Me.DataGrid1.Visible = True
Me.DataGrid1.Refresh
rs.Close
End Sub
3 输入关键字,查询效果如下: