在经典 ASP 脚本中使用 ADO.NET
我正在编写一些返回单个值的简单查询,并且我想从经典 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
命令对象以记录集形式返回数据。您可以从记录集字段获取值。在此示例中,我使用了定义的字段名称“MyCount”。如果你想要一个更通用的,你可以在最后一行获取 rs.fields(0) 的值。
快速示例:
可重用函数示例:
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:
Reusable function example: