如何判断数据库更新是否成功? [英] How to tell if a db update was successful?

查看:1057
本文介绍了如何判断数据库更新是否成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改一些遗留的ASP经典代码,我不太了解ASP。如何判断数据库插入,更新或删除是否失败? 失败,我的意思是它抛出一个错误或影响零行。

I'm trying to modify some legacy ASP classic code, and I don't know much about ASP. How do I tell if a database Insert, Update, or Delete failed? By 'failed', I mean it either threw an error or affected zero rows.

以下是已经在ASP文件中设置数据库连接的代码: / p>

Here's the code that was already in the ASP file that sets up the database connection:

On Error Resume Next
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "Provider=MSDAORA;Data Source=dbname;User Id=dbuser;Password=dbpw;"

有几个Select语句的工作方式如下:

There are a few Select statements that work like:

qry =  "select stuff here..."
Set objRs = objConn.Execute(qry)



我理解如何从Select语句获取结果,但是如何获取非Select语句的结果?使用结果集似乎不是正确的方法。或者是?

I understand how to get the results from the Select statements, but how do I get the results of a non-Select statement? Using a result set doesn't seem like it'd be the right way to do it. Or is it?

推荐答案

使用第一个执行方法参数受影响的行:

Use the first Execute method parameter to get the rows affected:

On Error resume next
Dim RecordsAffected as long
Dim cmd
Set cmd = server.createobject("ADODB.Command")

cmd.ActiveConnection = GetConnectionString()
cmd.CommandText = "Select stuff here"
cmd.CommandType = adCmdText

cmd.Execute RecordsAffected, , adExecuteNoRecords
If err.number > 0 or RecordsAffected = 0 then
    Response.Write "No record affected or SQL error or something"
end if

使用 adExecuteNoRecords 将根据 几个 来源

来源: MSDN中的执行方法

这篇关于如何判断数据库更新是否成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆