前段日子發表的文章,資料庫的連接代碼可以直接在ASP檔案中顯示出來。這次又進行了一次封裝。
開啟vb,新建Activex控件,工程名稱為WebDb,類別模組名稱為GetInfomation
引用」Microsoft Activex Data Object 2.6 Library ”
Private Conn As ADODB.Connection
Private Rs As ADODB.Recordset
'作用:判斷資料庫是否正確連結
'自己可以更改連接串
Public Function GetConn()
Conn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Northwind;Data Source=yang"
If Err.Number <> 0 Then
GetConn = False
Else
GetConn = True
End If
End Function
'根據輸入的僱員ID,得到僱員的名稱
Public Function GetEmployeeName(strEmployeeID As Integer) As String
Dim strSql As String
Set rs = New ADODB.Recordset
strSql = "select LastName+firstname from employees where EmployeeID=" & strEmployeeID
rs.Open strSql, Conn, adOpenStatic, adLockOptimistic
If rs.EOF Then
GetEmployeeName = ""
Else
GetEmployeeName = rs.Fields(0)
End If
rs.Close
End Function
'傳回所有的僱員清單
Public Function GetEmployeeList() As ADODB.Recordset
Dim strSql As String
Set rs = New ADODB.Recordset
strSql = "select EmployeeID,LastName,FirstName,Title,TitleOfCourtesy,BirthDate,HireDate,Address,City from employees" rs.CursorLocation,HireDate,Address,City from employees" rs.CursorLocation
, cireDate, adity),
cxUl.
Set GetEmployeeList = rs
'rs.Close
End Function
我們進行測試
新建ASP頁面,”TestWebDb1.asp”。主要用來測試GetEmployeeList()方法
<HEAD>
<!- 測試頁->
<!- 功能:測試組件->
<!- 作者:龍捲風.NET ->
<%
Dim strTopic
Dim strTitle
Dim strContents
Dim DataQuery
Dim Rs
Dim Myself
Myself=Request.ServerVariables("script_name")
Set DataQuery=Server.CreateObject("WebDb.GetInfomation") Set DataQuery=Server.CreateObject("WebDb.GetInfomation") Set DataQuery=Server.CreateObject("WebDb.GetInfomation") Set DataQuery=Server.CreateObject("WebDb.GetInfomation") Set DataQuery=Server.CreateObject("WebDb.GetInfomation") Set DataQuery=Server.CreateObject("WebDb.GetInfomation")
Set DataQuery=Server.CreateObject("WebDb.GetInfomation") Set DataQuery=Server。
%>
<TITLE>
資料組件測試頁
</TITLE>
<H1><CENTER>歡迎使用資料組件( www.downcodes.com)</CENTER></H1 >
<%
Dim Flag
Flag=DataQuery.GetConn()
If Flag=false then
ResPonse.Write "資料庫沒有連結,請檢查"
ResPonse.End
End if
Set Rs=DataQuery.GetEmployeeList()
if rs.eof then
Response.write "沒有資料,請查詢"
Response.end
end if
Rs.PageSize =3
Page= CLng(Request.QueryString ("Page"))
If Page < 1 Then Page = 1
If Page > Rs.PageCount Then Page = Rs.PageCount
Response.Write "<CENTER><TABLE BORDER=1 cellspacing=0 cellpadding=2>"
Response.Write "<tr BGCOLOR=silver align=center>"
Response.Write "<td>EmployeeID</TD>"
Response.Write
""<td>LastName</td>"
Response.Write "<td>FirstName</td>"
Response.Write "<td>Title</a></td>"
Response.Write "<td>TitleOfCourtesy</ a></td>"
Response.Write "<td>BirthDate</td>"
Response.Write "<td>HireDate</td>"
Response.Write "<td>Address</td>"
Response.Write " <td>City</td>"
Response.Write "</tr>"
Rs.AbsolutePage = Page
For iPage = 1 To Rs.PageSize
Response.Write "<TR align=right>"
for i=0 to Rs.fields.count-1
Response.Write "<td>"&Rs.fields.item( i)&"</td>"
next
Response.Write "</TR>"
Rs.MoveNext
If Rs.EOF Then Exit For
next
Response.Write "</TABLE></CENTER>"
%>
<Form name="myform" method="get">
<%If Page <> 1 Then%>
<A HREF="<%=Myself%>?Page=1">第一頁</A>
<A HREF ="<%=Myself%>?Page=<%=(Page-1)%>">上一頁</A>
<%End If%>
<%If Page <> Rs.PageCount Then%>
<A HREF="<%=Myself%>?Page=<%=(Page+1)%>">下一頁</A>
<A HREF="<%=Myself%>?Page=<%=Rs. PageCount%>">最後的一頁</A>
<%End If%>
頁次:<FONT COLOR="Red"><%=Page%>/<%=Rs.PageCount%></FONT>
< /Form>
<%
Rs.close
%>
新ASP頁面,”TestWebDb2.asp”。主要用來測試GetEmployeeName()方法
這個頁面相對簡單一些
<HEAD>
<!- 測試頁->
<!- 功能:測試組件->
<!- 作者:龍捲風.NET ->
<%
Dim DataQuery
Dim strID
Dim strResult
Set DataQuery=Server.CreateObject("WebDb.GetInfomation")
%>
<TITLE>
數據組件測試頁
</TITLE>
<H1><CENTER>歡迎使用資料元件( www.downcodes.com)</CENTER></H1 >
<%
If Len(Request.QueryString("ID")) > 0 Then
strID = Request. QueryString("ID")
Dim Flag
Flag=DataQuery.GetConn()
If Flag=false then
ResPonse.Write "資料庫沒有連結,請檢查"
ResPonse.End
End if
strResult=DataQuery.GetEmployeeName(cint(strID))
if strResult="" then
Response.Write "對不起,沒有這個編號,請查詢"
Response.End
else
ResPonse.Write strResult
end if
End If
%>
<FORM NAME="MyForm">
<INPUT TYPE=TEXTBOX NAME="EmpID" SIZE=40><P>
<INPUT LANGUAGE="VBScript"
TYPE="BUTTON"
VALUE="Search"
ONCLICK="window.location.href = 'TestWebDb2.asp?ID=' _
+ MyForm.EmpID.Value">
</FORM>
</HEAD>
一些想法:資料庫連結如果放到元件裡,如果要修改資料庫連結,則要重新編譯元件,可能會由此引發一些問題(但是推薦這種,畢竟資料庫名稱和伺服器名稱不會經常改變)
如果資料庫連結放到ASP頁面,可以透過屬性傳值到元件中,但是安全性會降低。
真是…
好了,這篇文章結束了,希望對大家有幫助
今後的話題便是如何實現AspToDll這個軟體的功能,我們一步一步實現。