明輝手游網(wǎng)中心:是一個免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

使用組件封裝數(shù)據(jù)庫設(shè)置(二)

[摘要]前段日子發(fā)表的文章,數(shù)據(jù)庫的連接代碼可以直接在ASP文件中顯示出來。這次又進(jìn)行了一次封裝。打開vb,新建Activex控件,工程名稱為WebDb,類模塊名稱為GetInfomation引用”Microsoft Activex Data Object 2.6 Library ”Private Con...

前段日子發(fā)表的文章,數(shù)據(jù)庫的連接代碼可以直接在ASP文件中顯示出來。這次又進(jìn)行了一次封裝。

打開vb,新建Activex控件,工程名稱為WebDb,類模塊名稱為GetInfomation

引用”Microsoft Activex Data Object 2.6 Library ”

Private Conn As ADODB.Connection

Private Rs As ADODB.Recordset


‘作用:判斷數(shù)據(jù)庫是否正確連結(jié)

'自己可以更改連接串

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


‘根據(jù)輸入的雇員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

 

我們進(jìn)行測試

新建ASP頁面,”TestWebDb1.asp”。主要用來測試GetEmployeeList()方法

<HEAD>

 

<!- 測試頁 ->

<!- 功能:測試組件 ->

<!- 作者:龍卷風(fēng).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>

   數(shù)據(jù)組件測試頁

</TITLE>

<H1><CENTER>歡迎使用數(shù)據(jù)組件(www.knowsky.com)</CENTER></H1>

<%

       Dim Flag

       Flag=DataQuery.GetConn()

       If Flag=false then

              ResPonse.Write "數(shù)據(jù)庫沒有連結(jié),請檢查"

              ResPonse.End

       End if

 

       Set Rs=DataQuery.GetEmployeeList()

       if rs.eof then

              Response.write "沒有數(shù)據(jù),請查詢"

              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>

 

<!- 測試頁 ->

<!- 功能:測試組件 ->

<!- 作者:龍卷風(fēng).NET ->

 

<%

    Dim DataQuery

       Dim strID

       Dim strResult

    Set DataQuery=Server.CreateObject("WebDb.GetInfomation")

%>

 

<TITLE>
數(shù)據(jù)組件測試頁
</TITLE>

<H1><CENTER>歡迎使用數(shù)據(jù)組件(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 "數(shù)據(jù)庫沒有連結(jié),請檢查"

                     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>

一些想法:數(shù)據(jù)庫連結(jié)如果放到組件里,如果要修改數(shù)據(jù)庫連結(jié),則要重新編譯組件,可能會由此引發(fā)一些問題(但是推薦這種,畢竟數(shù)據(jù)庫名和服務(wù)器名不會經(jīng)常改)

如果數(shù)據(jù)庫連結(jié)放到ASP頁面,可以通過屬性傳值到組件中,但是安全性會降低。

真是……

好了,這篇文章結(jié)束了,希望對大家有幫助

今后的話題便是如何實現(xiàn)AspToDll這個軟件的功能,我們一步一步實現(xiàn)。