Monday, December 17, 2007

How to delete a record in database

How to delete a record in database

S T E P - 1

- Firstly, we will need to create a RecordSet:

<%

Const adOpenKeyset = 1, adLockPessimistic = 2

Dim oConn

Dim oRs

Set oConn = Server.CreateObject("ADODB.Connection")

oConn.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("db1.mdb")

Set oRs = Server.CreateObject("ADODB.Recordset")

oRs.Open "SELECT * FROM member WHERE MemberID='" & strMemberID & "';", oConn, adOpenKeyset, adLockPessimistic 'Open "Member"table and found a single record based on the value of strMemberID

%>

S T E P - 2

- We need to check the record if it is existed or not.

- If the RecordCount = 0 , that means the record is not existed in the database:

<%

If oRs.RecordCount = 0 Then

Response.Write("The record is not found")

Response.End

End If

%>

S T E P - 3

- If the RecordCount <> 0 , we continue the Delete function:

<%

If oRs.RecordCount = 0 Then

Response.Write("The record is not found")

Response.End

Else

oRs.Delete 'Delete the record

End If

oRs.Close

Set oRs = Nothing

oConn.Close

Set oConn = nothing

%>

No comments: