0

我有asp頁面,我需要顯示來自Sql Server數據庫的數據表,我想從2個文本區域(源和目標)中選擇城市名稱,然後顯示所有該行的信息。 這裏是autocomplete.html的副本:jQuery從數據庫自動完成與asp

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8" /> 
     <title></title> 
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" /> 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script> 
<link rel="stylesheet" href="/resources/demos/style.css" /> 
<script> 
$(function() { 
var availableTags = [ 
"Shanghai", 
"Shenzhen", 
"Casablanca", 
"Jeddah", 
"Paris", 
"PHP", 
"London", 
"Tokyo", 
"Jeddah", 
"Istambul" 
]; 
$("#tags").autocomplete({ 
source: availableTags 
}); 
}); 
</script> 
    </head> 
    <body> 
    </body> 
</html> 

在頁面的index.asp:

.... 
    <!--#include file="autocomplete.html"--> <label for="tags"> POD </label> 
<input id="tags" /> 
    <br> 

我從jQuery自動完成想要得到的源,並從我的數據庫的目標不是方式:

var availableTags = [ 
"Shanghai", 
"Shenzhen", 
"Casablanca", 
"Jeddah", 
"Paris", 
"PHP", 
"London", 
"Tokyo", 
"Jeddah", 
"Istambul" 
]; 

我該怎麼做?

謝謝你的幫助!

回答

1

您可以像使用表單一樣使用經典ASP。嘗試把你的代碼括號中的數組列表裏面,像這樣......

var availableTags = [<%=GetCities() %>]; 

GetCities()功能會再輸出到Response對象所需的城市名單。

- 編輯 -

下面的代碼還沒有經過完全測試。您可能還需要修改適合您需求的零件:

const C_NO_DATA = "NO_DATA"    'Used when no data is returned to a consuming routine 
const C_ERROR = "ERROR"    'Used when an error is generated - to be fed to the comsuming routine 

'GetDataSet 
' Returns a table of data based on the supplied SQL statement and connection string. 
'Parameters: 
' sqlString (string) - The SQL string to be sent. 
' connString (string) - The database connection string. 
'Usage: 
' dataSet = GetDataSet(sqlString, connString) 
'Description: 
' This function generates a table of information in a 2 dimensional array. The first dimension represents the columns 
' and the second the rows. If an error occurs while the routine is executing the array and the base index (0,0) is set 
' to C_ERROR, (0,1) to the VBScript error index, and (0,2) to the VBScript error description. 
function GetDataSet(sqlString, connString) 
    'Initialise... 
    dim returnVal, rsData 
    on error resume next 
     'Define and open the recordset object... 
     set rsData = Server.CreateObject("ADODB.RecordSet") 
     rsData.Open sqlString, connString, 0, 1, 1 
     'Initialise an empty value for the containing array... 
     redim returnVal(0,0) 
     returnVal(0,0) = C_NO_DATA 
     'Deal with any errors... 
     if not rsData.EOF and not rsData.BOF then 
      'Store the data... 
      returnVal = rsData.GetRows() 
      'Tidy up... 
      rsData.close 
      set rsData = nothing 
      select case err.number 
       case 3021 'No data returned 
        'Do nothing as the initial value will still exist (C_NO_DATA) 
       case 0  'No error 
        'Do nothing as data has been returned 
       case else 
        redim returnVal(4,0) 
        returnVal(0,0) = C_ERROR 
        returnVal(1,0) = err.number 
        returnVal(2,0) = err.description 
        returnVal(3,0) = sqlString 
        returnVal(4,0) = connString 
      end select 
     end if 
    on error goto 0 
    'Return the array... 
    GetDataSet = returnVal 
end function 

function GetCities() 
    dim sql, tCity, rc, max, rv 
    sql = _ 
     "SELECT " & _ 
      "'""' + city_name + '""' AS city " & _ 
     "FROM " & _ 
      "clkj_freight " & _ 
     "WHERE " & _ 
      "pol = '" & replace(request.querystring("q"), "'", "''") & "'" 
    tCity = GetDataSet(sql, conn) 
    if tCity(0, 0) = C_ERROR or tCity(0, 0) = C_NO_DATA then rv = tcity(0, 0) 
    max = UBound(tCity, 2) '2 is the second dimension of the array 
    for rc = 0 to max 
     Response.Write(tCity(rc, 0)) 
     If rc<max then Response.Write(",") 
    Next 'rc 
    'If an error is encountered or no data is returned then notify the user (this behaviour may be changed if necessary)... 
    GetCities = rv 
end function 
+0

GetCities()函數應該怎麼做?我的連接頁面是這樣的:'response.expires = -1 set rs = server.CreateObject(「adodb.recordset」) sql =「SELECT * FROM clkj_freight WHERE POL =」 sql = sql&「'」&request .sysystring(「q」)&「'」 rs.open sql,conn,1,1' 這是正確的嗎? – tollamie 2013-05-06 08:17:43

+0

如果該代碼檢索您的城市列表,那麼您只需將它們讀入數組,在每個條目的開頭和結尾添加語音標記,然後使用「Join」(使用逗號作爲分隔符)以製作你的城市名單。 (你甚至可以把語音標記放到SQL中來加快速度 – Paul 2013-05-06 12:53:16

+0

你可以給我一個關於如何做到這一點的代碼示例,因爲我真的不知道該怎麼辦? – tollamie 2013-05-07 01:59:22