当前位置: 首页 > 图文教程 > 网络编程 > ASP > 使用组件封装数据库操作(二)

ASP
保护 XML Web 服务免受黑客攻击
开发BtoC电子商务系统(ASP.NET)
用asp.net实现的把本文推荐给好友功能
尝尝ASP.NET中的小甜饼
web.config一个中文解释
使用ASP.NET加密口令
在SQL Server中保存和输出图片
ASP 中健壮的页结构的异常处理
如何使你的机器运行ASP?
asp(Active Server Page)的语言特性
关于inc文件
IE4 的 模 式 对 话 框 设 计
亲密接触ASP.net(6)
亲密接触ASP.Net(7)
用ASP.Net写一个发送ICQ信息的程序
用ASP.Net编写的查询域名的程序
使用 ASP+ 列表绑定控件(上)
使用 ASP+ 列表绑定控件(中)
使用 ASP+ 列表绑定控件(下)
揭开ASP神秘面纱(1)

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 170 ::
收藏到网摘: n/a

 

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