在经典 ASP 脚本中使用 ADO.NET

发布于 2024-09-19 11:20:23 字数 684 浏览 3 评论 0原文

我正在编写一些返回单个值的简单查询,并且我想从经典 ASP 的 ADO 库中获取 ADO.NET ExecuteScalar 方法的行为。但是,我不想重新发明轮子。是否可以在经典 ASP 中实例化 ADO.NET 的 Command 对象?如果是这样,我将如何去做呢?

如果我确实需要重新实现此功能,最好的方法是什么?

这是我想要做的(在 VBScript 中):

set cmd = Server.CreateObject("ADODB.Command")
cmd.ActiveConnection = conn
cmd.CommandText = "SELECT COUNT(*) FROM corp_crcchallenge WHERE cc_email = ?"
appendParam cmd, "email", adVarChar, adParamInput, 255, email
emailFound = cmd.ExecuteScalar()

appendParam 是我用来附加参数的子例程,这样我就不必在这个应用程序的其他部分重复太多。它使它更容易阅读。

我认为如果我需要重新实施,类似下面的内容可能是正确的方向。 (又是 VBScript)

emailFound = cmd.Execute(Nothing, Nothing, adExecuteRecord)

I'm writing some simple queries which return a single value and I'd like to get the ADO.NET ExecuteScalar method's behavior from Classic ASP's ADO libary. However, I'd prefer to not re-invent the wheel. Is it possible to instantiate ADO.NET's Command object within classic ASP? If so, how would I go about doing that?

If I do need to re-implement this feature, what is the best way to do that?

Here's what I'm trying to do (In VBScript):

set cmd = Server.CreateObject("ADODB.Command")
cmd.ActiveConnection = conn
cmd.CommandText = "SELECT COUNT(*) FROM corp_crcchallenge WHERE cc_email = ?"
appendParam cmd, "email", adVarChar, adParamInput, 255, email
emailFound = cmd.ExecuteScalar()

appendParam is a subroutine I made to append the param so I wouldn't have to repeat myself too much in another part of this app. It makes it a bit easier to read.

I'm thinking that something like the below might be the right direction if I need to re-implement. (Again VBScript)

emailFound = cmd.Execute(Nothing, Nothing, adExecuteRecord)

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

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

发布评论

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

评论(1

独孤求败 2024-09-26 11:20:23

命令对象以记录集形式返回数据。您可以从记录集字段获取值。在此示例中,我使用了定义的字段名称“MyCount”。如果你想要一个更通用的,你可以在最后一行获取 rs.fields(0) 的值。

快速示例:

set cmd = Server.CreateObject("ADODB.Command")
set rs = server.createObject("ADODB.RecordSet")

with cmd
 .ActiveConnection = conn
 .CommandText = "SELECT COUNT(*) as MyCount FROM corp_crcchallenge WHERE cc_email = @p1"
 .createparameter("@p1",adVArChar,adParamInput,,email)
 set rs = .Execute
end with

rtn = rs.fields("MyCount")

可重用函数示例:

n = MyScalar("SELECT COUNT(*) FROM corp_crcchallenge WHERE cc_email = @p1","[email protected]")

function MyScalar(sql, Param1)
    set cmd = Server.CreateObject("ADODB.Command")
    set rs = server.createObject("ADODB.RecordSet")

    with cmd
        .ActiveConnection = conn
        .CommandText = sql
        .createparameter("@p1",adVArChar,adParamInput,,Param1)
        set rs = .Execute
    end with

    MyScalar = rs.fields(0)

end function

The command object returns data as recordset. You can get the value from the recordsets field. In this example, I have used a defined field name of "MyCount". If you wanted a more generic, you could just get the value of rs.fields(0) in the last line.

Quick Example:

set cmd = Server.CreateObject("ADODB.Command")
set rs = server.createObject("ADODB.RecordSet")

with cmd
 .ActiveConnection = conn
 .CommandText = "SELECT COUNT(*) as MyCount FROM corp_crcchallenge WHERE cc_email = @p1"
 .createparameter("@p1",adVArChar,adParamInput,,email)
 set rs = .Execute
end with

rtn = rs.fields("MyCount")

Reusable function example:

n = MyScalar("SELECT COUNT(*) FROM corp_crcchallenge WHERE cc_email = @p1","[email protected]")

function MyScalar(sql, Param1)
    set cmd = Server.CreateObject("ADODB.Command")
    set rs = server.createObject("ADODB.RecordSet")

    with cmd
        .ActiveConnection = conn
        .CommandText = sql
        .createparameter("@p1",adVArChar,adParamInput,,Param1)
        set rs = .Execute
    end with

    MyScalar = rs.fields(0)

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