从 ADODB 获取受影响的行数ASP 与 JScript

发布于 2025-01-07 04:09:46 字数 1057 浏览 0 评论 0原文

可能的重复:
如何我使用 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 中的原始类型总是按值调用。

Possible Duplicate:
How can I get the # of rows affected by a statement using ADO with JavaScript?

We're using MS-SQL7.0, ASP(with Jscript)
there isn't any problem in querying and executing.
But we faced an problem getting affected record count.
We refer to this source

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

Here is our source code

    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);

But this code don't work, rowCount are set to its initial value(0).
I think it because primitive type in javascript is always called by value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心不设防 2025-01-14 04:09:46

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

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

In the past I've tried two methods in such a case (I agree, a little bit scratchy.).

1. Mixing languages

<%@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 features can be useful. (you did this)

<%@Language=JScript%>
<%
// 
// ..
var query = "...";
//..
this.db.Execute(query);
this.rowCount = this.db.Execute("Select @@ROWCOUNT").Fields.Item(0).Value;
//..
this.db.Close();
//..
%>
花落人断肠 2025-01-14 04:09:46

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

The Execute method of the ActiveX Data Objects (ADO) Command object passes by reference an integer value that you can use to retrieve the number of records affected by a SQL UPDATE command.

#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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文