使用组件封装数据库操作(二)

2008-02-23 05:29:13来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

前段日子发表的文章,数据库的连接代码可以直接在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%>

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:使用组件封装数据库操作(一)

下一篇:如何在pb中创建COM组件,并在asp中调用并返回结果集?