2013-07-30 56 views
1

所以我有一個名爲BrowseAllItems.asp的頁面,它顯示我所有的數據庫信息以及重定向到EditItem.asp的單獨編輯按鈕(部分顯示如下),以便編輯或刪除特定記錄的數據庫信息。編輯數據的部分工作正常,但我可以使用一些幫助刪除記錄。以下是我有:如何使用asp從訪問數據庫中刪除特定的記錄?

Sub DeleteRecord 

ItemCode=request.form("item_code") 


'Create an ADO connection object 
Dim Connection 
Set Connection = Server.CreateObject("ADODB.Connection") 

'To open an Access database our string would look like the following: 
Dim ConnectionString 
ConnectionString = "DRIVER={Microsoft Access Driver (*.mdb)};" &_ 
       "DBQ=" & Server.MapPath("\") & "\" & FolderName & "\items.mdb;DefaultDir=;UID=;PWD=;" 

'Then to open the database we (optionally) set some of the properties of the Connection and call Open 
Connection.ConnectionTimeout = 30 
Connection.CommandTimeout = 80 
Connection.Open ConnectionString 

'Create an ADO recordset object 
Dim rs 
Set rs = Server.CreateObject("ADODB.Recordset") 

'Initialise the strSQL variable with an SQL statement to query the database 
strSQL = "SELECT * FROM items WHERE ITEM_CODE='" & ItemCode & "' " 

我知道它與上面的行做的事情 - 此代碼的工作,如果我取代「ItemCode」與備案的實際項目代碼,但我需要一種方法來取來自所選記錄的項目代碼,並將其應用於ItemCode。

rs.LockType = 3 
'Open the recordset with the SQL query 
rs.Open strSQL, Connection 
'Tell the recordset we are deleting a record 
rs.Delete 

rs.Close 
'Reset server objects 
Set rs = Nothing 
Set Connection = Nothing 

Response.Write "Record Deleted" 

End Sub 

回答

2

也許我誤解的東西,但它看起來像一個參數查詢的ADODB.Command也許會有幫助。我不明白你爲什麼需要一個記錄集來刪除給定的記錄ITEM_CODE

看來你已經有了一個可用的ADODB.Connection。在本例中,我使用cnn而不是連接作爲對象變量的名稱。

Dim cmd ' As ADODB.Command 
Dim lngRecordsAffected ' As Long 
strSQL = "PARAMETERS which_item Text(255);" & vbCrLf & _ 
    "DELETE FROM items WHERE ITEM_CODE = [which_item];" 
Set cmd = Server.CreateObject("ADODB.Command") 
cmd.CommandType = 1 ' adCmdText 
Set cmd.ActiveConnection = cnn 
cmd.CommandText = strSQL 
'cmd.Parameters.Append cmd.CreateParameter("which_item", _ 
' adVarChar, adParamInput, 255, ItemCode) 
cmd.Parameters.Append cmd.CreateParameter("which_item", _ 
    200, 1, 255, ItemCode) 
cmd.Execute lngRecordsAffected 
Set cmd = Nothing 
' lngRecordsAffected is the number of records deleted in case you 
' need it for anything ... perhaps you'd like to do this ... 
Response.Write lngRecordsAffected & " Record(s) Deleted" 
0

在我看來,你正在失去「ItemCode」的價值。你是否發佈了你的要求? request.form涉及post的操作。另一方面,request.querystring從查詢字符串(get動作)和request("ItemCode")收到數據捕獲這兩種情況。

在任何情況下 you must sanitize!!! before a concatenation

爲了防止SQL注入,你的情況ausuming即ItemCode是一個整數,你可以使用cint這樣

ItemCode = cint(request("item_code")) 

我猜你的查詢不拋出錯誤,因爲的存在帶有「ItemCode」空值的單引號提供了一個不返回數據的有效SQL語句。在調試的時候,你總是可以在response.write strSQL之前執行(或打開)這個句子。

相關問題