asp快速開發(fā)方法之?dāng)?shù)據(jù)操作
當(dāng)前位置:點(diǎn)晴教程→知識管理交流
→『 技術(shù)文檔交流 』
我的目的是讓開發(fā)變得簡單,盡可能少地考慮實(shí)現(xiàn)語句,更多地把精力用于思考業(yè)務(wù)邏輯。希望我的文章對大家有所啟發(fā)和幫助。 先看以下例子: <% db_path = "database/cnbruce.mdb" set conn= server.createobject("adodb.connection") connstr = "provider=microsoft.jet.oledb.4.0;data source="&server.mappath(db_path) conn.open connstr set rs = server.createobject ("adodb.recordset") sql = "select * from cnarticle" rs.open sql,conn,1,1 if rs.eof and rs.bof then response.write ("暫時(shí)還沒有文章") else do until rs.eof response.write("文章標(biāo)題是:"& rs("cn_title")) response.write(" 文章作者是:"& rs("cn_author")) response.write(" 文章加入時(shí)間是:"& rs("cn_time")) response.write(" 文章內(nèi)容是:"& rs("cn_content")) response.write(" ") rs.movenext loop end if rs.close set rs = nothing conn.close set conn=nothing %>
<% db_path = "database/cnbruce.mdb" set conn= server.createobject("adodb.connection") connstr = "provider=microsoft.jet.oledb.4.0;data source="&server.mappath(db_path) conn.open connstr %>
<% set rs = server.createobject ("adodb.recordset") sql = "select * from cnarticle" rs.open sql,conn,1,1 if rs.eof and rs.bof then response.write ("暫時(shí)還沒有文章") else do until rs.eof response.write("文章標(biāo)題是:"& rs("cn_title")) response.write(" 文章作者是:"& rs("cn_author")) response.write(" 文章加入時(shí)間是:"& rs("cn_time")) response.write(" 文章內(nèi)容是:"& rs("cn_content")) response.write(" ") rs.movenext loop end if rs.close set rs = nothing conn.close set conn=nothing %>
<% dim conn dim rs sub closedatabase conn.close set conn = nothing end sub sub opendatabase dim strserver,struid,strsapwd,strdbname strserver="192.168.1.1" '數(shù)據(jù)庫服務(wù)器名 struid="sa" '您的登錄帳號 strsapwd="" '您的登錄密碼 strdbname="cnbruce.mdb" '您的數(shù)據(jù)庫名稱 set conn = server.createobject("adodb.connection") '用于連接access conn.connectionstring = "provider=microsoft.jet.oledb.4.0; data source=" & server.mappath(strdbname) '用于連接mssql 'conn.connectionstring = "driver={sql server};driver={sql server};server="&strserver&";uid="&struid&";pwd="&strsapwd&";database="&strdbname set rs=server.createobject("adodb.recordset") conn.open if err then err.clear set conn = nothing gbl_chk_tempstr = gbl_chk_tempstr & "數(shù)據(jù)庫連接錯(cuò)誤!" response.write gbl_chk_tempstr response.end end if end sub %> 現(xiàn)在我們的showit.asp可以這樣寫: showit.asp <% sql = "select * from cnarticle" opendatabase rs.open sql,conn,1,1 if not rs.eof then do until rs.eof response.write("文章標(biāo)題是:"& rs("cn_title")) response.write(" 文章作者是:"& rs("cn_author")) response.write(" 文章加入時(shí)間是:"& rs("cn_time")) response.write(" 文章內(nèi)容是:"& rs("cn_content")) response.write(" ") rs.movenext loop else response.write ("暫時(shí)還沒有文章") end if closedatabase %> 嗯,我們又少寫了一些東西,這樣是最簡單的嗎?當(dāng)然不是!還可以更簡單。 使用getrows把查詢出來的數(shù)據(jù)傳給一個(gè)變量,使用ubound方法取得數(shù)據(jù)記錄條數(shù)。 不明白?沒關(guān)系,讓我們繼續(xù)往下看: 再建個(gè)文件:sql.asp sql.asp <% class selectdatatable public function selectdata(sql) if sql<>"" then opendatabase rs.open sql,conn,1,1 if not rs.eof then thedata=rs.getrows(-1) closedatabase else closedatabase end if end if selectdata=thedata end function end class %> 嗯,復(fù)制它就可以了,現(xiàn)在我們的showit.asp可以簡單地這樣寫: showit.asp <% sql = "select * from cnarticle" set loaddata=new selectdatatable thedata=loaddata.selectdata(sql) if isarray(thedata) then num=ubound(thedata,2) for i=0 to num response.write("文章標(biāo)題是:"& thedata(1,i)) response.write(" 文章作者是:"& thedata(2,i)) response.write(" 文章加入時(shí)間是:"& thedata(3,i)) response.write(" 文章內(nèi)容是:"& thedata(4,i)) response.write(" ") next else response.write("暫時(shí)還沒有文章") end if %> 呵呵,這樣,我們只要用兩句語句就完成了數(shù)據(jù)的讀取。同樣的,通過在sql.asp中加入 <% public function selectdatanum(sql) if sql<>"" then opendatabase rs.open sql,conn,1,1 if not rs.eof then thedata=rs.getrows(-1) closedatabase num=ubound(thedata,2) else closedatabase end if end if selectdatanum=num end function %> 我們就可以使用 <% sql = "select * from cnarticle" set loaddata=new selectdatatable num=loaddata.selectdatanum(sql) %> 來取得記錄條數(shù),可以用于分頁或者用戶名是否重復(fù)的判斷。 其它的對數(shù)據(jù)記錄的操作我們新建一個(gè)類,使用updatetable來完成操作: <% class updatatable public function updatasql(sql) if sql<>"" then opendatabase conn.execute(sql) closedatabase end if end function end class %> <% sql = "delete from cnarticle" set updatedate=new updatatable updatedate.updatasql(sql) %> 當(dāng)然你也這以這樣寫: <% sql="insert into cnarticle(cn_title,cn_author,cn_content) values(' "&whattitle&" ',' "&whoauthor&" ',' "&whatcontent&" ')" opendatabase conn.execute(sql) closedatabase %> 考慮到可能刪除語句我們會(huì)這么寫: sql="delect from cnarticle where id in(1,3,5,6,7,8)" 我新建一個(gè)類deldatatable,直接使用deldatatable.deldatasql(tablename,delfield,id)完成記錄的刪除操作。 <% class deldatatable dim tempvalue public function deldatasql(tablename,delfield,id) if tablename<>"" and id<>"" then sql="delete from "&tablename if isnumeric(id) and instr(id,",")=0 then sql = sql & " where "&delfield&" = "&id else sql = sql & " where "&delfield&" in ("& id &")" end if opendatabase conn.execute(sql) closedatabase tempvalue=true else tempvalue=false end if deldatasql=tempvalue end function end class %> 以下是我的sql.asp文件,請自己進(jìn)行增刪 復(fù)制代碼 <% '用于查詢數(shù)據(jù) class selectdatatable '查出記錄 public function selectdata(sql) if sql<>"" then opendatabase rs.open sql,conn,1,1 if not rs.eof then thedata=rs.getrows(-1) closedatabase else closedatabase end if end if selectdata=thedata end function '查出記錄條數(shù) public function selectdatanum(sql) if sql<>"" then opendatabase rs.open sql,conn,1,1 if not rs.eof then thedata=rs.getrows(-1) closedatabase num=ubound(thedata,2) else closedatabase end if end if selectdatanum=num end function '使用select count(*) from tablename 查出記錄有數(shù) public function selectcountnum(sql) if sql<>"" then opendatabase rs.open sql,conn,1,1 if not rs.eof then thedata=rs.getrows(-1) closedatabase num=thedata(0,0) else closedatabase end if end if selectcountnum=num end function '將查詢的數(shù)據(jù)全部生成隱藏值 public function gethiddendata(sql) dim tempvalue if sql<>"" then opendatabase rs.open sql,conn,1,1 if not rs.eof then thedata=rs.getrows(-1) thefieldcount=rs.fields.count for i=0 to thefieldcount-1 thefieldlist = thefieldlist & rs.fields(i).name & "," next closedatabase thefield = split(thefieldlist,",") for i=0 to thefieldcount-1 tempvalue = tempvalue & "" next else closedatabase end if end if gethiddendata=tempvalue end function end class class updatatable public function updatasql(sql) if sql<>"" then opendatabase conn.execute(sql) closedatabase end if end function end class class deldatatable dim tempvalue public function deldatasql(tablename,delfield,id) if tablename<>"" and id<>"" then sql="delete from "&tablename if isnumeric(id) and instr(id,",")=0 then sql = sql & " where "&delfield&" = "&id else sql = sql & " where "&delfield&" in ("& id &")" end if opendatabase conn.execute(sql) closedatabase tempvalue=true else tempvalue=false end if deldatasql=tempvalue end function end class %> 該文章在 2010/7/3 13:37:46 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |