前段日子發表的文章,數據庫的連接代碼可以直接在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 = adUseClIEnt
rs.Open strSql, Conn, adOpenStatic
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 Rs=Server.CreateObject("adodb.recordset")
%>
<TITLE>
數據組件測試頁
</TITLE>
<H1><CENTER>歡迎使用數據組件(www.knowsky.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.knowsky.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這個軟件的功能,我們一步一步實現。