2011-06-12 191 views
0

我有一個小問題,vbscript 我已經聲明瞭一些函數,從一個sql查詢中傳遞結果集,就像參數一樣,問題是函數showData接受的參數不像一個對象結果集傳遞函數參數的問題vbscript

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 

有誰能告訴我這裏的問題在哪裏。我是vbscript的新手。這是與對象objRS功能showData錯誤

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

回答

3

的主要問題是返回一個對象的引用函數必須使用Set他們返回值:

function get_rows_from_to(from,size,conn) 
    ' ... 
    set get_rows_from_to = result 
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&"") 
    set get_rows_from_to = result 
end function 

和等效於此

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()已經返回一個ADODB.RecordSet。之前通過Server.CreateObject()創建一個空白的是沒有意義的。實際上,全部你的Set xyz = Server.CreateObject("ADODB.Recordset")系都是多餘的。

在函數中爲返回值創建單獨的變量沒有意義。該功能返回值:

function getFoo() 
    getFoo = "foo" 
end function 

兩個最後提示:

  • 始終使用Option Explicit
  • 總是對輸出到頁面的任何一段數據都使用Server.HtmlEncode()。這比所有其他提示更重要。
+0

使用'Set'的提示是lifer-saver ... +1 – Dabbler 2012-11-17 21:12:03