3、 Add方法
Add方法的目的是向數據庫中添加一條新的CD信息,因此它打開的記錄集要求能夠更新。這就不能像OpenDB中那樣僅用Open打開數據集。詳細的代碼見下面:
PRivate Sub Add(Id As String, Name As String, Author As String, Price As Currency, Information As String)
Dim NewConn As New ADODB.Connection
Dim NewRecordsetCD As New ADODB.Recordset
NewConn.Open "Provider=SQLOLEDB;DataSource=MUSE;Initial Catalog=Music;UserId=Wayne;PassWord=AbCdEfG"
NewRecordsetCD.Open "SELECT * FROM CD", NewConn, adOpenDynamic, adLockOptimistic
NewRecordsetCD.AddNew
NewRecordsetCD.FIElds("CD_ID") = Id
NewRecordsetCD.FIElds("CD_Name") = Name
NewRecordsetCD.FIElds("CD_Author") = Author
NewRecordsetCD.FIElds("CD_Price") = Price
NewRecordsetCD.FIElds("CD_Information") = Information
NewRecordsetCD.Update
End Sub
解釋:使用 NewRecordsetCD.AddNew,
NewRecordsetCD.FIElds(“Some_Key”)=Some_Value
NewRecordsetCD.Update 來添加一條紀錄
4、 Delete方法:
這個方法的目的是為了刪除庫中的某條CD名和相關的信息。它的實現方法是通過查詢表中唯一的CD_ID來定位CD信息,從而刪除該行紀錄
Private Sub Delete(Id As String)
Dim NewConn As New ADODB.Connection
Dim RecordsetDelete As New ADODB.Recordset
NewConn.Open "Provider=SQLOLEDB;DataSource=MUSE;Initial Catalog=Music;UserId=Wayne;PassWord=AbCdEfG"
’從表中刪除一條信息
NewConn.Execute "DELETE FROM CD WHERE CD_ID ='" & Id & "'", adCmdText + adExecuteNoRecords
End Sub