从ADODB&中获取受影响的行数使用JScript的ASP

时间:2012-02-20 03:59:58

标签: asp-classic adodb jscript

  

可能重复:
  How can I get the # of rows affected by a statement using ADO with 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中的原始类型总是按值调用。

2 个答案:

答案 0 :(得分:0)

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

<强> 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();
//..
%>

答案 1 :(得分:-1)

ActiveX数据对象(ADO)Command对象的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