明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

手把手教你使用VB來(lái)創(chuàng)建ASP組件(4) 

[摘要]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