Encapsulation | data | database | Component encapsulation using components to encapsulate Database operations (ii)
Previous days published articles, the database connection code can be directly displayed in the ASP file. This time, another package was made.
Open VB, new ActiveX control, project name is WebDB, class module name is Getinfomation
Reference "Microsoft Activex Data Object 2.6 Library"
Private Conn as ADODB. Connection
Private Rs as ADODB. Recordset
' Role: Determine if the database is properly connected
' I can change the connection string
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
' The name of the employee according to the employee ID entered
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
' Return all Employees list
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
We do the tests.
New ASP page, "Testwebdb1.asp". Mainly used to test the Getemployeelist () method
<HEAD>
<!-test Page->
<!-function: Test component->
<!-Author: Tornado. 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>
Data Component Test Page
</TITLE>
<H1><CENTER> Welcome to use data Components (www.ourfly.com) </CENTER></H1>
<%
Dim Flag
Flag=dataquery.getconn ()
If Flag=false Then
Response.Write "Database is not connected, please check"
Response.End
End If
Set rs=dataquery.getemployeelist ()
If Rs.eof Then
Response.Write "No data, please inquire"
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.pag