手把手教你使用VB來(lái)創(chuàng)建ASP組件(4)
發(fā)表時(shí)間:2023-08-11 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]3、 Add方法 Add方法的目的是向數(shù)據(jù)庫(kù)中添加一條新的CD信息,因此它打開(kāi)的記錄集要求能夠更新。這就不能像OpenDB中那樣僅用Open打開(kāi)數(shù)據(jù)集。詳細(xì)的代碼見(jiàn)下面:Private Sub A...
3、 Add方法
Add方法的目的是向數(shù)據(jù)庫(kù)中添加一條新的CD信息,因此它打開(kāi)的記錄集要求能夠更新。這就不能像OpenDB中那樣僅用Open打開(kāi)數(shù)據(jù)集。詳細(xì)的代碼見(jiàn)下面:
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 來(lái)添加一條紀(jì)錄
4、 Delete方法:
這個(gè)方法的目的是為了刪除庫(kù)中的某條CD名和相關(guān)的信息。它的實(shí)現(xiàn)方法是通過(guò)查詢表中唯一的CD_ID來(lái)定位CD信息,從而刪除該行紀(jì)錄
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