通用網(wǎng)頁查詢函數(shù)的設計與應用

時間:2022-10-08 04:07:00

導語:通用網(wǎng)頁查詢函數(shù)的設計與應用一文來源于網(wǎng)友上傳,不代表本站觀點,若需要原創(chuàng)文章可咨詢客服老師,歡迎參考。

通用網(wǎng)頁查詢函數(shù)的設計與應用

1.引言

隨著人們對信息化潮流的逐漸認識,各種信息在Internet上的和檢索就變得非常的重要。特別是使用B/S模式的信息和檢索,由于它的簡單性、靈活性,越來越得到了廣泛的應用?,F(xiàn)在,經(jīng)常被人們采用的B/S模式的開發(fā)工具有ASP、PHP、JSP等,這些開發(fā)工具簡單易學、功能強大,為各種信息在Internet上的和檢索提供了強有力的幫助。但是,隨著信息量的逐漸爆炸,如果仍然采用針對特定信息設計專用程序的傳統(tǒng)辦法的話,顯然將導致信息的效率非常地低。

為此,筆者研究了一種通用信息與檢索的技術方案,使用這套方案,將極大地減輕開發(fā)者的工作量,節(jié)約成本。本文介紹的便是這套技術方案中的一個非常重要的組成部分——基于ASP的通用網(wǎng)頁查詢函數(shù)設計應用

2.通用網(wǎng)頁查詢函數(shù)的設計

通用網(wǎng)頁查詢函數(shù)的功能主要是:根據(jù)調(diào)用者提供的各種參數(shù),在后臺數(shù)據(jù)庫管理系統(tǒng)中進行檢索,最后將檢索結果以二維數(shù)組的形式返回給調(diào)用者。

為了實現(xiàn)上述功能,實現(xiàn)該函數(shù)的主要思想是:根據(jù)調(diào)用者提供的各種參數(shù),生成對應的SQL語句,接下來與后臺數(shù)據(jù)庫管理系統(tǒng)建立連接、提取數(shù)據(jù)、斷開連接,最后將檢索結果以二維數(shù)組的形式返回給調(diào)用者。

該函數(shù)的輸入?yún)?shù)有:后臺數(shù)據(jù)庫管理系統(tǒng)的代號(如0代表SQLServer、1代表VFP等)、數(shù)據(jù)源名、表名(可以是單表,也可以是多表連接)或視圖名、all/distinct關鍵字、top關鍵字、字段名數(shù)組、where條件、groupby子句/orderby字句、檢索結果存放的二維數(shù)組名等。

該函數(shù)的返回值為邏輯型,True代表查詢過程中未出現(xiàn)錯誤,否則,若為False說明查詢過程中出現(xiàn)了錯誤。

下面給出經(jīng)調(diào)試過的通用網(wǎng)頁查詢函數(shù)的源代碼。

functiondata_getting(param_database_code,param_dsn_name,param_table_name,

param_all,param_top,param_field_names(),ByRefdata(),param_condition,

param_other,ByRefrcount,ByReffcount,ByReffieldsname())

onerrorresumenext

''''生成查詢語句

ifparam_all=truethen

query="select"

else

query="selectdistinct"

endif

query=query+param_top+""

ifparam_field_names(0)="*"then''''查詢?nèi)孔侄?/p>

query=query+"*"

else

d_g_i=0

fcount=0

foreachiteminparam_field_names

ifparam_field_names(d_g_i)<>""then

query=query+param_field_names(d_g_i)+","

fcount=fcount+1''''記錄集列數(shù)

endif

d_g_i=d_g_i+1

next

endif

query=left(query,len(query)-1)+"from"+param_table_name''''去掉最后一個逗號(全部字段:去掉空格)

iflen(param_condition)>0then

query=query+"where"+param_condition

endif

iflen(param_other)>0then

query=query+""+param_other

endif

''''打開記錄集

setconntemp=server.createobject("ADODB.Connection")

conntemp.ConnectionString=param_dsn_name

conntemp.Open

conntemp.errors.clear

Setrstemp=Server.CreateObject("ADODB.Recordset")

rstemp.ActiveConnection=conntemp

ifparam_database_code=0then‘代表后臺數(shù)據(jù)庫是SQLSERVER

rstemp.CursorType=3

rstemp.LockType=1

elseifparam_database_code=1then‘代表后臺數(shù)據(jù)庫是VFP

rstemp.CursorType=1

rstemp.LockType=1

else

…‘代表后臺數(shù)據(jù)庫是其它數(shù)據(jù)庫管理系統(tǒng)(代碼略)

endif

rstemp.Source=query

rstemp.open

‘判斷查詢過程中是否出現(xiàn)錯誤

ifconntemp.errors.count>0then

data_getting=false

rcount=0

else

‘如果查詢過程中未出現(xiàn)錯誤,將查詢結果存放到指定的二維數(shù)組中

rcount=rstemp.recordcount''''記錄集行數(shù)

ifrcount=0then

data_getting=true

else

data_getting=true

ifparam_field_names(0)="*"then''''查詢?nèi)孔侄?/p>

fcount=rstemp.fields.count

endif

ReDimdata(rcount-1,fcount-1),fieldsname(fcount-1)

ford_g_i=1torcount

ford_g_j=1tofcount

data(d_g_i-1,d_g_j-1)=trim(rstemp.fields(d_g_j-1).value)

ifd_g_i=1then

fieldsname(d_g_j-1)=rstemp.fields(d_g_j-1).name

endif

next

rstemp.movenext

next

endif

endif

rstemp.close

setrstemp=nothing

conntemp.close

setconntemp=nothing

endfunction

3.通用網(wǎng)頁查詢函數(shù)的應用

在筆者參與的各類基于Web的數(shù)據(jù)庫應用系統(tǒng)的開發(fā)過程中,全部使用了前文給出的通用網(wǎng)頁查詢函數(shù)。利用<!--#includefile="data_getting.inc"-->語句(通用網(wǎng)頁查詢函數(shù)被保存為一個獨立的文件data_getting.inc),在需要進行數(shù)據(jù)查詢的網(wǎng)頁中,嵌入該函數(shù),然后在ASP頁面只需調(diào)用該函數(shù),就能得到希望得到的查詢結果,從而大大減輕了編程的工作量,并便于ASP頁面的簡化和美化。

4.結束語

本文給出的通用網(wǎng)頁查詢函數(shù),以及筆者開發(fā)的其它的一些通用函數(shù)(用于執(zhí)行一系列SQL命令的函數(shù)、用于調(diào)用數(shù)據(jù)庫端存儲過程的函數(shù)等),大大減輕了在系統(tǒng)開發(fā)過程中的工作量,基本實現(xiàn)了代碼復用的目的,希望能給從事數(shù)據(jù)庫應用系統(tǒng)的開發(fā)者一些有益的啟示。

參考文獻:

1.劉福太等譯ASP3高級編程機械工業(yè)出版社2000年

2.彭萬波等著ASP開發(fā)基礎與范例電子工業(yè)出版社2002年