asp快速開發方法之數據操作
當前位置:點晴教程→知識管理交流
→『 技術文檔交流 』
[p]我的目的是讓開發變得簡單,盡可能少地考慮實現語句,更多地把精力用于思考業務邏輯。希望我的文章對大家有所啟發和幫助。[/p]
[p]先看以下例子:[/p] <%[br]db_path = "database/cnbruce.mdb"[br]set conn= server.createobject("adodb.connection")[br]connstr = "provider=microsoft.jet.oledb.4.0;data source="&server.mappath(db_path)[br]conn.open connstr[br]set rs = server.createobject ("adodb.recordset")[br]sql = "select * from cnarticle"[br]rs.open sql,conn,1,1[br]if rs.eof and rs.bof then[br]response.write ("暫時還沒有文章")[br]else[br]do until rs.eof[br]response.write("文章標題是:"& rs("cn_title"))[br]response.write(" 文章作者是:"& rs("cn_author"))[br]response.write(" 文章加入時間是:"& rs("cn_time"))[br]response.write(" 文章內容是:"& rs("cn_content"))[br]response.write(" ")[br]rs.movenext[br]loop[br]end if[br]rs.close [br]set rs = nothing[br]conn.close [br]set conn=nothing[br]%> [p][br]嗯,這是一個典型的讀取數據并顯示的例子,參見:[url=http://cnbruce.com/blog/showlog.asp?cat_id=26&log_id=448]http://cnbruce.com/blog/showlog.asp?cat_id=26&log_id=448[/url][br]嗯,確實簡單。從上至下,很容易明白。但是當你對多個表進行讀插刪改的時候,當你的代碼里有很多html\js混雜的時候,你會有疑問:為什么有這么多東西要重復呢?[br]所以一般我們把一些簡單的操作獨立出來,寫成類或者函數放進包含文件(include)。[br][br]那么以上的操作我們可以使用兩個文件來實現:[br]conn.asp[/p] <%[br]db_path = "database/cnbruce.mdb"[br]set conn= server.createobject("adodb.connection")[br]connstr = "provider=microsoft.jet.oledb.4.0;data source="&server.mappath(db_path)[br]conn.open connstr[br]%> [p][br]showit.asp[/p] [br]<%[br]set rs = server.createobject ("adodb.recordset")[br]sql = "select * from cnarticle"[br]rs.open sql,conn,1,1[br]if rs.eof and rs.bof then[br]response.write ("暫時還沒有文章")[br]else[br]do until rs.eof[br]response.write("文章標題是:"& rs("cn_title"))[br]response.write(" 文章作者是:"& rs("cn_author"))[br]response.write(" 文章加入時間是:"& rs("cn_time"))[br]response.write(" 文章內容是:"& rs("cn_content"))[br]response.write(" ")[br]rs.movenext[br]loop[br]end if[br]rs.close [br]set rs = nothing[br]conn.close [br]set conn=nothing[br]%> [p][br]現在相對簡單多了,如果有多個操作頁面我們只要導入連接文件就可以了,不過還是不夠簡潔,哪里不簡潔?[br]一直在創建server,一直在寫close,這樣很容易出錯,并且看起來與內容無關的太多。[br][br]那我再改進下:[br]把conn.asp文件改成:[/p] [p][/p] <%[br]dim conn[br]dim rs[br]sub closedatabase[br] conn.close[br] set conn = nothing[br]end sub[br]sub opendatabase[br] dim strserver,struid,strsapwd,strdbname[br] strserver="192.168.1.1" '數據庫服務器名[br] struid="sa" '您的登錄帳號[br] strsapwd="" '您的登錄密碼[br] strdbname="cnbruce.mdb" '您的數據庫名稱[br] set conn = server.createobject("adodb.connection")[br] '用于連接access[br] conn.connectionstring = "provider=microsoft.jet.oledb.4.0; data source=" & server.mappath(strdbname)[br] '用于連接mssql[br] 'conn.connectionstring = "driver={sql server};driver={sql server};server="&strserver&";uid="&struid&";pwd="&strsapwd&";database="&strdbname[br] set rs=server.createobject("adodb.recordset")[br] conn.open[br] if err then[br] err.clear[br] set conn = nothing[br] gbl_chk_tempstr = gbl_chk_tempstr & "數據庫連接錯誤!"[br] response.write gbl_chk_tempstr[br] response.end[br] end if [br]end sub[br]%> 現在我們的showit.asp可以這樣寫:[br]showit.asp[br] [br]<%[br]sql = "select * from cnarticle"[br]opendatabase[br]rs.open sql,conn,1,1[br]if not rs.eof then[br] do until rs.eof[br] response.write("文章標題是:"& rs("cn_title"))[br] response.write(" 文章作者是:"& rs("cn_author"))[br] response.write(" 文章加入時間是:"& rs("cn_time"))[br] response.write(" 文章內容是:"& rs("cn_content"))[br] response.write(" ")[br] rs.movenext[br] loop[br]else[br] response.write ("暫時還沒有文章")[br]end if[br]closedatabase[br]%>[br]嗯,我們又少寫了一些東西,這樣是最簡單的嗎?當然不是!還可以更簡單。[br]使用getrows把查詢出來的數據傳給一個變量,使用ubound方法取得數據記錄條數。[br]不明白?沒關系,讓我們繼續往下看:[br][br]再建個文件:sql.asp[br]sql.asp[br] <%[br]class selectdatatable[br] public function selectdata(sql)[br] if sql<>"" then[br] opendatabase[br] rs.open sql,conn,1,1[br] if not rs.eof then[br] thedata=rs.getrows(-1)[br] closedatabase[br] else[br] closedatabase[br] end if[br] end if[br] selectdata=thedata[br] end function[br]end class[br]%>[br]嗯,復制它就可以了,現在我們的showit.asp可以簡單地這樣寫:[br][br]showit.asp[br] [br][br]<%[br]sql = "select * from cnarticle"[br]set loaddata=new selectdatatable[br]thedata=loaddata.selectdata(sql)[br]if isarray(thedata) then[br] num=ubound(thedata,2)[br] for i=0 to num[br] response.write("文章標題是:"& thedata(1,i))[br] response.write(" 文章作者是:"& thedata(2,i))[br] response.write(" 文章加入時間是:"& thedata(3,i))[br] response.write(" 文章內容是:"& thedata(4,i))[br] response.write(" ")[br] next[br]else[br] response.write("暫時還沒有文章")[br]end if[br]%>[br]呵呵,這樣,我們只要用兩句語句就完成了數據的讀取。同樣的,通過在sql.asp中加入[br] <%[br] public function selectdatanum(sql)[br] if sql<>"" then[br] opendatabase[br] rs.open sql,conn,1,1[br] if not rs.eof then[br] thedata=rs.getrows(-1)[br] closedatabase[br] num=ubound(thedata,2)[br] else[br] closedatabase[br] end if[br] end if[br] selectdatanum=num[br] end function[br]%>[br]我們就可以使用[br] <%[br]sql = "select * from cnarticle"[br]set loaddata=new selectdatatable[br]num=loaddata.selectdatanum(sql)[br]%>[br]來取得記錄條數,可以用于分頁或者用戶名是否重復的判斷。[br][br]其它的對數據記錄的操作我們新建一個類,使用updatetable來完成操作:[br] <%[br]class updatatable[br] public function updatasql(sql)[br] if sql<>"" then[br] opendatabase[br] conn.execute(sql)[br] closedatabase[br] end if[br] end function[br]end class[br]%>[br] <%[br]sql = "delete from cnarticle"[br]set updatedate=new updatatable[br]updatedate.updatasql(sql)[br]%> 當然你也這以這樣寫:[br] <%[br]sql="insert into cnarticle(cn_title,cn_author,cn_content) values(' "&whattitle&" ',' "&whoauthor&" ',' "&whatcontent&" ')"[br]opendatabase[br]conn.execute(sql)[br]closedatabase[br]%>[br]考慮到可能刪除語句我們會這么寫:[br]sql="delect from cnarticle where id in(1,3,5,6,7,8)"[br][br]我新建一個類deldatatable,直接使用deldatatable.deldatasql(tablename,delfield,id)完成記錄的刪除操作。[br] <%[br]class deldatatable[br] dim tempvalue[br] public function deldatasql(tablename,delfield,id)[br] if tablename<>"" and id<>"" then[br] sql="delete from "&tablename[br] if isnumeric(id) and instr(id,",")=0 then[br] sql = sql & " where "&delfield&" = "&id[br] else[br] sql = sql & " where "&delfield&" in ("& id &")"[br] end if[br] opendatabase[br] conn.execute(sql)[br] closedatabase[br] tempvalue=true[br] else[br] tempvalue=false[br] end if[br] deldatasql=tempvalue[br] end function[br]end class[br]%>[br]以下是我的sql.asp文件,請自己進行增刪[br]復制代碼 <%[br]'用于查詢數據[br]class selectdatatable[br] '查出記錄[br] public function selectdata(sql)[br] if sql<>"" then[br] opendatabase[br] rs.open sql,conn,1,1[br] if not rs.eof then[br] thedata=rs.getrows(-1)[br] closedatabase[br] else[br] closedatabase[br] end if[br] end if[br] selectdata=thedata[br] end function[br] '查出記錄條數[br] public function selectdatanum(sql)[br] if sql<>"" then[br] opendatabase[br] rs.open sql,conn,1,1[br] if not rs.eof then[br] thedata=rs.getrows(-1)[br] closedatabase[br] num=ubound(thedata,2)[br] else[br] closedatabase[br] end if[br] end if[br] selectdatanum=num[br] end function[br] '使用select count(*) from tablename 查出記錄有數[br] public function selectcountnum(sql)[br] if sql<>"" then[br] opendatabase[br] rs.open sql,conn,1,1[br] if not rs.eof then[br] thedata=rs.getrows(-1)[br] closedatabase[br] num=thedata(0,0)[br] else[br] closedatabase[br] end if[br] end if[br] selectcountnum=num[br] end function[br] '將查詢的數據全部生成隱藏值[br] public function gethiddendata(sql)[br] dim tempvalue[br] if sql<>"" then[br] opendatabase[br] rs.open sql,conn,1,1[br] if not rs.eof then[br] thedata=rs.getrows(-1)[br] thefieldcount=rs.fields.count[br] for i=0 to thefieldcount-1[br] thefieldlist = thefieldlist & rs.fields(i).name & ","[br] next[br] closedatabase[br] thefield = split(thefieldlist,",")[br] for i=0 to thefieldcount-1[br] tempvalue = tempvalue & ""[br] next[br] else[br] closedatabase[br] end if[br] end if[br] gethiddendata=tempvalue[br] end function[br] [br]end class[br]class updatatable[br] public function updatasql(sql)[br] if sql<>"" then[br] opendatabase[br] conn.execute(sql)[br] closedatabase[br] end if[br] end function[br] [br]end class[br]class deldatatable[br] dim tempvalue[br] public function deldatasql(tablename,delfield,id)[br] if tablename<>"" and id<>"" then[br] sql="delete from "&tablename[br] if isnumeric(id) and instr(id,",")=0 then[br] sql = sql & " where "&delfield&" = "&id[br] else[br] sql = sql & " where "&delfield&" in ("& id &")"[br] end if[br] opendatabase[br] conn.execute(sql)[br] closedatabase[br] tempvalue=true[br] else[br] tempvalue=false[br] end if[br] deldatasql=tempvalue[br] end function[br]end class[br]%> 該文章在 2010/7/3 13:37:46 編輯過 |
關鍵字查詢
相關文章
正在查詢... |