vb 6.0数据库操作代码例子
\’删除函数
\’************************************************************
Private Sub Delete_Click()
\’声明数据库连接对象
Dim conn As New ADODB.Connection
conn.ConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DB.mdb;Persist Security Info=False“
\’打开数据库连接
conn.Open
\’执行删除sql语句
conn.Execute “Delete from Users where UserName=\’insert\’ or UserName=\’update\’“
\’关闭数据库并释放数据库对象
conn.Close
Set conn = Nothing
End Sub
\’***********************************************************
\’添加函数
\’************************************************************
Private Sub InsertButton_Click()
\’声明数据库连接对象
Dim conn As New ADODB.Connection
conn.ConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DB.mdb;Persist Security Info=False“
\’打开数据库连接
conn.Open
\’执行添加sql语句
conn.Execute “Insert into Users (UserName,UserPassword) values (\’insert\’,\’test\’)“
\’关闭数据库并释放数据库对象
conn.Close
Set conn = Nothing
End Sub
\’***********************************************************
\’读取函数
\’************************************************************
Private Sub SelectButton_Click()
\’声明数据库连接对象和记录集对象
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
\’指定连接字符串
conn.ConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DB.mdb;Persist Security Info=False“
\’打开数据库连接
conn.Open
\’执行查询sql语句
rs.Open “Select * from Users“, conn
\’读取记录集内容
While Not rs.EOF
\’显示记录
Print rs(“UserId“), rs(“UserName“), rs(“UserPassword“)
rs.MoveNext
Wend
\’关闭记录集并释放记录集对象
rs.Close
Set rs = Nothing
\’关闭数据库并释放数据库对象
conn.Close
Set conn = Nothing
End Sub
\’***********************************************************
\’更新函数
\’************************************************************
Private Sub Update_Click()
\’声明数据库连接对象
Dim conn As New ADODB.Connection
conn.ConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DB.mdb;Persist Security Info=False“
\’打开数据库连接
conn.Open
\’执行更新sql语句
conn.Execute “Update Users set UserName=\’update\’ where UserName=\’insert\’“
\’关闭数据库并释放数据库对象
conn.Close
Set conn = Nothing
End Sub