這是我自己的心得,給大家作個參考。
我的目的是讓開發變得簡單,盡可能少地考慮實現語句,更多地把精力用于思考業務邏輯。希望我的文章對大家有所啟發和幫助。
如果你對ASP不熟悉,可以先看以下教程:
1、http://布魯斯狼的ASP編程入門進階
2、www.w3schools.com的ASP教程(英文\中文),查看更多
好吧,讓我們進入正題:
先看以下例子:
<%
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 ("暫時還沒有文章")
else
Do Until rs.EOF
response.write("文章標題是:"& rs("cn_title"))
response.write("<br>文章作者是:"& rs("cn_author"))
response.write("<br>文章加入時間是:"& rs("cn_time"))
response.write("<br>文章內容是:"& rs("cn_content"))
response.write("<hr>")
rs.MoveNext
Loop
end if
rs.close
Set rs = Nothing
conn.close
set conn=Nothing
%>
嗯,這是一個典型的讀取數據并顯示的例子,參見:http://www.cnbruce.com/blog/showlog.asp?cat_id=26&log_id=448
嗯,確實簡單。從上至下,很容易明白。但是當你對多個表進行讀插刪改的時候,當你的代碼里有很多HTML\js混雜的時候,你會有疑問:為什么有這么多東西要重復呢?
所以一般我們把一些簡單的操作獨立出來,寫成類或者函數放進包含文件(include)。
那么以上的操作我們可以使用兩個文件來實現:
conn.asp
<%
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
%>
showit.asp
<!--#include file="conn.asp" -->
<%
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 ("暫時還沒有文章")
else
Do Until rs.EOF
response.write("文章標題是:"& rs("cn_title"))
response.write("<br>文章作者是:"& rs("cn_author"))
response.write("<br>文章加入時間是:"& rs("cn_time"))
response.write("<br>文章內容是:"& rs("cn_content"))
response.write("<hr>")
rs.MoveNext
Loop
end if
rs.close
Set rs = Nothing
conn.close
set conn=Nothing
%>
參考:http://www.cnbruce.com/blog/showlog.asp?cat_id=26&log_id=448
現在相對簡單多了,如果有多個操作頁面我們只要導入連接文件就可以了,不過還是不夠簡潔,哪里不簡潔?
一直在創建server,一直在寫close,這樣很容易出錯,并且看起來與內容無關的太多。
那我再改進下:
把conn.asp文件改成:
復制代碼代碼如下:
<%
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" '數據庫服務器名
StrUid="sa" '您的登錄帳號
StrSaPwd="" '您的登錄密碼
StrDbName="cnbruce.mdb" '您的數據庫名稱
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 & "數據庫連接錯誤!"
Response.Write GBL_CHK_TempStr
Response.End
End If
End Sub
%>
現在我們的showit.asp可以這樣寫:
showit.asp
<!--#include file="conn.asp" -->
<%
sql = "Select * from cnarticle"
opendatabase
rs.Open sql,conn,1,1
If not Rs.eof then
Do Until rs.EOF
response.write("文章標題是:"& rs("cn_title"))
response.write("<br>文章作者是:"& rs("cn_author"))
response.write("<br>文章加入時間是:"& rs("cn_time"))
response.write("<br>文章內容是:"& rs("cn_content"))
response.write("<hr>")
rs.MoveNext
Loop
else
response.write ("暫時還沒有文章")
end if
Closedatabase
%>
嗯,我們又少寫了一些東西,這樣是最簡單的嗎?當然不是!還可以更簡單。
使用GetRows把查詢出來的數據傳給一個變量,使用ubound方法取得數據記錄條數。
不明白?沒關系,讓我們繼續往下看:
再建個文件: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
%>
嗯,復制它就可以了,現在我們的showit.asp可以簡單地這樣寫:
showit.asp
<!--#include file="conn.asp" -->
<!--#include file="sql.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("文章標題是:"& Thedata(1,i))
response.write("<br>文章作者是:"& Thedata(2,i))
response.write("<br>文章加入時間是:"& Thedata(3,i))
response.write("<br>文章內容是:"& Thedata(4,i))
response.write("<hr>")
next
else
response.write("暫時還沒有文章")
End If
%>
呵呵,這樣,我們只要用兩句語句就完成了數據的讀取。同樣的,通過在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)
%>
來取得記錄條數,可以用于分頁或者用戶名是否重復的判斷。
其它的對數據記錄的操作我們新建一個類,使用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)
%>
當然你也這以這樣寫:
<%
sql="insert into cnarticle(cn_title,cn_author,cn_content) values(' "&whattitle&" ',' "&whoauthor&" ',' "&whatcontent&" ')"
opendatabase
conn.execute(sql)
closedatabase
%>
考慮到可能刪除語句我們會這么寫:
sql="delect from cnarticle where id in(1,3,5,6,7,8)"
我新建一個類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文件,請自己進行增刪
復制代碼 <%
'用于查詢數據
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