简体   繁体   English

问题通过 function arguments vbscript

[英]problem passing function arguments vbscript

I have a little problem with vbscript I have declared some function with passing the result set from a sql query like an argument and the problem is that the function showData accept the argument like an object not like a resultset I have a little problem with vbscript I have declared some function with passing the result set from a sql query like an argument and the problem is that the function showData accept the argument like an object not like a resultset

function get_count(conn)
    dim query, size
    Set query = Server.CreateObject("ADODB.Recordset")
    set query = conn.Execute("select count(*) as size from doctor")
    size = query.Fields("size")
    get_count = size
end function
function get_rows_from_to(from,size,conn)
    dim result
    Set result = Server.CreateObject("ADODB.Recordset")
    set result = conn.Execute("SELECT name, surname,family,egn,citizenship FROM doctor limit "&from&","&size&"")
    get_rows_from_to = result
end function
Sub showData(objRS)
    dim isEven
    isEven = false
    Response.Write "<table>"
    Response.Write "<tr>"
    Response.Write "<th >Име</th><th >Презиме</th><th >Фамилия</th><th >ЕГН</th><th >Гражданство</td>"
    Response.Write "</tr>"
    While Not objRS.EOF
        Response.Write "<tr>"
        if isEven then
            Response.Write "<td style = 'background-color :#e9ebf2'>"&objRS.Fields("name")&"</td>"&_
                "<td style = 'background-color :#e9ebf2'>"&objRS.Fields("surname")&"</td>"&_
                "<td style = 'background-color :#e9ebf2'>"&objRS.Fields("family")&"</td>"&_
                "<td style = 'background-color :#e9ebf2'>"&objRS.Fields("egn")&"</td>"&_
                "<td style = 'background-color :#e9ebf2'>"&objRS.Fields("citizenship")&"</td>"
            isEven = false
        else
            Response.Write "<td>"&objRS.Fields("name")&"</td><td>"&objRS.Fields("surname")&"</td><td>"&objRS.Fields("family")&"</td><td>"&objRS.Fields("egn")&"</td><td>"&objRS.Fields("citizenship")&"</td>"
            isEven = true
        end if
        Response.Write "</tr>"
        objRS.MoveNext
    Wend 
    Response.Write "</table>"
    objRS.Close
end sub
const ROWS_PER_PAGE = 10
Dim sConnection, conn , result,pages,selPage,from,lenght,pos,size

from = 0
lenght = 10
pos = 1
size = 0
sConnection = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER=localhost; DATABASE=docunion; UID=root;PASSWORD=root; OPTION=3" 

Set conn = Server.CreateObject("ADODB.Connection") 
Set result = Server.CreateObject("ADODB.Recordset")
conn.Open sConnection

size = get_count (conn)
size = CInt(size)
if size  > 0 then
    pages = size / 10
    lenght = (ROWS_PER_PAGE*pos)
    set result = get_rows_from_to(from,lenght,conn)
    from = lenght + 1
    pos = pos + 1
    showData(result)
else
    Set result = Nothing
    conn.Close
    Set conn = Nothing
end if

Could anyone tell me where is the problem here.谁能告诉我这里的问题出在哪里。 I'm new in vbscript.我是 vbscript 的新手。 This is the error in the function showData with the object objRS这是 function showData 与 object objRS 中的错误

Microsoft VBScript runtime error '800a01b6'
Object doesn't support this property or method: 'EOF'
/index.asp, line 57

The main problem is that functions that return an object reference must use Set for their return value:主要问题是返回 object 引用的函数必须使用Set作为其返回值:

function get_rows_from_to(from,size,conn)
    ' ...
    set get_rows_from_to = result
end function

A few other hints:其他一些提示:

There is no need to declare functions before you use them.在使用函数之前无需声明函数。 You can put all your function declarations at the end of the script and keep the main script body at the top.您可以将所有 function 声明放在脚本的末尾,并将主脚本主体放在顶部。

This is needlessly redundant:这是不必要的冗余:

function get_rows_from_to(from,size,conn)
    dim result
    Set result = Server.CreateObject("ADODB.Recordset")
    set result = conn.Execute("SELECT name, surname,family,egn,citizenship FROM doctor limit "&from&","&size&"")
    set get_rows_from_to = result
end function

and is equivalent to this相当于这个

function get_rows_from_to(from,size,conn)
    set get_rows_from_to = conn.Execute("SELECT name, surname,family,egn,citizenship FROM doctor limit " & from & "," & size &"")
end function 

conn.Execute() already returns an ADODB.RecordSet. conn.Execute()已返回 ADODB.RecordSet。 It makes no sense to create a blank one via Server.CreateObject() before.之前通过Server.CreateObject()创建一个空白是没有意义的。 In fact, all your Set xyz = Server.CreateObject("ADODB.Recordset") lines are superfluous.实际上,您所有Set xyz = Server.CreateObject("ADODB.Recordset")行都是多余的。

There is no sense in creating a separate variable for the return value in Functions.在函数中为返回值创建一个单独的变量是没有意义的。 The function is its return value: function它的返回值:

function getFoo()
    getFoo = "foo"
end function

Two final tips:最后的两个提示:

  • Always use Option Explicit .始终使用Option Explicit
  • Always use Server.HtmlEncode() on any piece of data you output to the page.始终对您 output 到页面的任何数据块使用Server.HtmlEncode() This is even more important than all the other tips.这比所有其他技巧更重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM