views:

58

answers:

1

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  A: 

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
bugtussle
Thanks, I ended up arriving at that solution on my own. (after posting the question) But I was really hoping that I could just do a `Server.CreateObject("ADODOTNET.Command")` or something and have the better API to work with. Can anyone explain to me why I can't?
sholsinger
You can, see here: http://dskc.wordpress.com/2007/12/06/calling-net-component-from-a-classic-asp-page/ and here: http://weblogs.asp.net/dneimke/archive/2004/01/31/65330.aspx
bugtussle
its called "Com interop" http://msdn.microsoft.com/en-us/library/kew41ycz%28VS.71%29.aspx
bugtussle