经典ASP中的二进制IP搜索

时间:2013-12-09 22:59:27

标签: sql-server asp-classic

我有一个带有CIDR块的表,我已经将它们转换为二进制(4)中的StartIP和EndIP。我想用用户提供的IP(IPV4)搜索该表,并返回IP落在StartIP和EndIP之间的任何记录。

有关背景信息,请参阅:Convert IP to 32-bit Binary in TSQL

以下适用于TSQL(返回1条记录):

declare @Binary varbinary(25)
select @Binary = MyDB.dbo.fnBinaryIPv4Start('204.228.241.25')

select field1, field2 from MyDB.dbo.MyTable WHERE (@Binary >= IPStartBinary AND @Binary <= IPEndBinary)

但是,这不会返回Classic ASP中的任何记录:

Set cmd = Server.CreateObject("ADODB.Command")
            cmd.CommandType = adCmdText
            cmd.ActiveConnection =objConn

            strSQL2 = "SELECT MyDB.dbo.fnBinaryIPV4Start(?)"

            'IP Parameter
            cmd.Parameters.Append cmd.CreateParameter("@IP",adVarChar,adParamInput,15,strIP)
            cmd("@IP") = strIP

            cmd.CommandText = strSQL2
            strBinary = cmd.Parameters(0)

            Set cmd = Server.CreateObject("ADODB.Command")
            cmd.CommandType = adCmdText
            cmd.ActiveConnection = objConn
            cmd.CommandTimeOut = 1200    

            strSQL = "SELECT field1, field2 FROM MyDB.dbo.MyTable WHERE (? >= IPStartBinary AND ? <= IPEndBinary) "
            cmd.Parameters.Append (cmd.CreateParameter("IP", adVarBinary, adParamInput, 50, strBinary))
            cmd.Parameters.Append (cmd.CreateParameter("IP", adVarBinary, adParamInput, 50, strBinary))
        End If

cmd.CommandText = strSQL
set objRS = cmd.Execute
if not objRS.EOF then
arrMyResults = objRS.GetRows()
strMyRewultsUbound = UBound(arrMyResults,2)
end if

objRS.close
set objRS = nothing

有什么建议吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

我不明白你的asp代码的第一部分?你永远不会执行strSQL2?所以你永远不会把ip作为二进制文件?尝试Response.write strBinary变量