[英]How to tell if a call to SQL Execute() using ADO fails in classic ASP
我有以下縮短的經典ASP代碼,使SQL插入調用...
cmd.CommandText = "insert into MyTable values(blah, blah, blah)"
cmd.CommandType = adCmdText
Set rs = cmd.Execute()
使用Execute()返回的記錄集如何判斷插入是否成功?
檢查Err對象
cmd.CommandText = "insert into MyTable values(blah, blah, blah)"
cmd.CommandType = adCmdText
On Error Resume Next
Set rs = cmd.Execute()
If Err.number<>0 or objConnection.Errors.Count <> 0 Then
'Do something to handle the error
End If
On Error Goto 0
還可以在15秒內查看此鏈接
如果您不想檢查錯誤,可以:
cmd.CommandText = "insert into MyTable values(blah, blah, blah) SELECT @@Rowcount"
cmd.CommandType = adCmdText
Set rs = cmd.Execute()
RecordsAffected = rs(0)
在同一個waw中,如果你有一個標識列,你可以使用獲得結果id
cmd.CommandText = "insert into MyTable values(blah, blah, blah) SELECT @@Identity"
cmd.CommandType = adCmdText
Set rs = cmd.Execute()
NewID = rs(0)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.