1

可能重复:
如何获取受使用 ADO 和 JavaScript 的语句影响的行数?

我们使用的是MS-SQL7.0,ASP(带Jscript)查询和执行没有任何问题。但是我们遇到了影响记录数的问题。我们参考这个来源

http://support.microsoft.com/kb/195048

这是我们的源代码

    var query = "...";
    this.db = Server.CreateObject("ADODB.Connection");
    this.db.Open(this.connectionString);
    this.db.Execute(query, this.rowCount);
    Response.Write(this.rowCount);

    or

    var query = "...";
    this.db = Server.CreateObject("ADODB.Connection");
    this.cmd  = Server.CreateObject("ADODB.Command");
    this.cmd.ActiveConnection = this.db;
    this.cmd.CommandText = query;
    this.cmd.Execute(this.rowCount);
    Response.Write(this.rowCount);

但是这段代码不起作用,rowCount被设置为其初始值(0)。我认为这是因为 javascript 中的原始类型总是按值调用。

4

2 回答 2

1

过去我在这种情况下尝试过两种方法(我同意,有点刺耳。)。

1.混合语言

<%@Language=JScript%>
<%
// 
// ..
this.query = "..."; // required
this.rowCount = 0; // required

ExecCommand(this);

//..
this.db.Close();
//..
%>
<script language="vbscript" runat="server">
Sub ExecCommand(obj)
    Dim intAffectedRows
    obj.db.Execute obj.query, intAffectedRows
    obj.rowCount = intAffectedRows 'assign rowCount
End Sub
</script>

2. RDBMS 特性很有用。(你做了这个)

<%@Language=JScript%>
<%
// 
// ..
var query = "...";
//..
this.db.Execute(query);
this.rowCount = this.db.Execute("Select @@ROWCOUNT").Fields.Item(0).Value;
//..
this.db.Close();
//..
%>
于 2012-02-20T07:22:41.830 回答
-1

ActiveX 数据对象 (ADO) 命令对象的 Execute 方法通过引用传递一个整数值,您可以使用该整数值来检索受 SQL UPDATE 命令影响的记录数。

#DEFINE adModeReadWrite 3
#DEFINE adCmdText 1

oConnection = CREATEOBJECT("ADODB.Connection")
oCommand = CREATEOBJECT("ADODB.Command")

lcConnString = "DRIVER={SQL Server};" + ;
   "SERVER=YourServerName;" + ;
   "DATABASE=pubs"

lcUID = "YourUserID"
lcPWD = "YourPassword"

oConnection.ATTRIBUTES = adModeReadWrite
oConnection.OPEN(lcConnString, lcUID, lcPWD )

* Use the command object to perform an UPDATE
* and return the count of affected records.
strSQL = "UPDATE roysched SET royalty = royalty * 1.5"
liRecordsAffected = 0
WITH oCommand
   .CommandType = adCmdText
   .ActiveConnection = oConnection
   .CommandText = strSQL
   .Execute(@liRecordsAffected)
ENDWITH
=MESSAGEBOX("Records affected: " + LTRIM(STR(liRecordsAffected)))

* Set the royalty column back to its previous value.
strSQL = "UPDATE roysched SET royalty = royalty / 1.5"
liRecordsAffected = 0
WITH oCommand
   .CommandType = adCmdText
   .ActiveConnection = oConnection
   .CommandText = strSQL
   .Execute(@liRecordsAffected)
ENDWITH


=MESSAGEBOX("Records affected: " + LTRIM(STR(liRecordsAffected)))

http://support.microsoft.com/kb/195048

于 2012-02-20T04:28:18.597 回答