2013-08-15
展开全部
ADO控件与数据库连接代码如下
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub Command1_Click()
'插入数据:
If rs.State <> adStateClosed Then '检查记录集状态
rs.Close
End If
rs.Open "insert into T_user(name,age) values('" & txtname.Text & "'," & txtage & ")", conn, adOpenDynamic, adLockBatchOptimistic
If rs.State <> adStateClosed Then
rs.Close
End If
'更新DataGrid1数据
rs.Open "select * from T_user", conn, adOpenDynamic, adLockBatchOptimistic
Set DataGrid1.DataSource = rs
End Sub
Private Sub Form_Load()
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\hb\桌面\db1.mdb;Persist Security Info=False"
conn.CursorLocation = adUseClient '游标放在客户端
rs.CursorLocation = adUseClient
rs.Open "select * from T_user", conn, adOpenDynamic, adLockBatchOptimistic
Set DataGrid1.DataSource = rs
End Sub
Private Sub Form_Unload(Cancel As Integer)
rs.Close '释放
conn.Close
Set rs = Nothing
Set conn = Nothing
End Sub
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub Command1_Click()
'插入数据:
If rs.State <> adStateClosed Then '检查记录集状态
rs.Close
End If
rs.Open "insert into T_user(name,age) values('" & txtname.Text & "'," & txtage & ")", conn, adOpenDynamic, adLockBatchOptimistic
If rs.State <> adStateClosed Then
rs.Close
End If
'更新DataGrid1数据
rs.Open "select * from T_user", conn, adOpenDynamic, adLockBatchOptimistic
Set DataGrid1.DataSource = rs
End Sub
Private Sub Form_Load()
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\hb\桌面\db1.mdb;Persist Security Info=False"
conn.CursorLocation = adUseClient '游标放在客户端
rs.CursorLocation = adUseClient
rs.Open "select * from T_user", conn, adOpenDynamic, adLockBatchOptimistic
Set DataGrid1.DataSource = rs
End Sub
Private Sub Form_Unload(Cancel As Integer)
rs.Close '释放
conn.Close
Set rs = Nothing
Set conn = Nothing
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询