使用組件封裝資料庫操作(二)

來源:互聯網
上載者:User
封裝|資料|資料庫|組件封裝 使用組件封裝資料庫操作(二)
前段日子發表的文章,資料庫的串連代碼可以直接在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.ourfly.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.Pag



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。