'*********************************************************** '刪除函數(shù) '************************************************************ Private Sub Delete_Click() '聲明數(shù)據(jù)庫連接對象 Dim conn As New ADODB.Connection conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DB.mdb;Persist Security Info=False" '打開數(shù)據(jù)庫連接 conn.Open '執(zhí)行刪除sql語句 conn.Execute "Delete from Users where UserName='insert' or UserName='update'" '關(guān)閉數(shù)據(jù)庫并釋放數(shù)據(jù)庫對象 conn.Close Set conn = Nothing End Sub '*********************************************************** '添加函數(shù) '************************************************************ Private Sub InsertButton_Click() '聲明數(shù)據(jù)庫連接對象 Dim conn As New ADODB.Connection conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DB.mdb;Persist Security Info=False" '打開數(shù)據(jù)庫連接 conn.Open '執(zhí)行添加sql語句 conn.Execute "Insert into Users (UserName,UserPassword) values ('insert','test')" '關(guān)閉數(shù)據(jù)庫并釋放數(shù)據(jù)庫對象 conn.Close Set conn = Nothing End Sub '*********************************************************** '讀取函數(shù) '************************************************************ Private Sub SelectButton_Click() '聲明數(shù)據(jù)庫連接對象和記錄集對象 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" '打開數(shù)據(jù)庫連接 conn.Open '執(zhí)行查詢sql語句 rs.Open "Select * from Users", conn '讀取記錄集內(nèi)容 While Not rs.EOF '顯示記錄 Print rs("UserId"), rs("UserName"), rs("UserPassword") rs.MoveNext Wend '關(guān)閉記錄集并釋放記錄集對象 rs.Close Set rs = Nothing '關(guān)閉數(shù)據(jù)庫并釋放數(shù)據(jù)庫對象 conn.Close Set conn = Nothing End Sub '*********************************************************** '更新函數(shù) '************************************************************ Private Sub Update_Click() '聲明數(shù)據(jù)庫連接對象 Dim conn As New ADODB.Connection conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DB.mdb;Persist Security Info=False" '打開數(shù)據(jù)庫連接 conn.Open '執(zhí)行更新sql語句 conn.Execute "Update Users set UserName='update' where UserName='insert'" '關(guān)閉數(shù)據(jù)庫并釋放數(shù)據(jù)庫對象 conn.Close Set conn = Nothing End Sub |
|
來自: 戰(zhàn)神之家 > 《我的圖書館》