Utilize ADO.NET within Classic ASP script

↘锁芯ラ 提交于 2020-01-02 09:57:10

问题


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)

回答1:


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","abc@def.com")

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


来源:https://stackoverflow.com/questions/3702697/utilize-ado-net-within-classic-asp-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!