top
Loading...
如何用VB.Net創建一個三層的數據庫應用程序
1.概論:

本文將介紹如何創建一個三層應用程序,并且將介紹如何創建一個Web Service服務。

ADO.NET創建Windows三層結構應用程序的體系架構如下圖所示:





該結構分三個層次:表示層、業務層、數據層。

數據層:代表物理數據庫。

業務層:負責數據層與表示層之間的數據傳輸。

表示層:應用程序的客戶端,它通過業務層來訪問數據庫。

表示層所操作的是駐留在內存中的本地數據,當需要更新數據庫數據時,要通過業務層提供的更新方法實現。這樣可以大大提高應用程序的性能,而且,什么時候更新數據完全由你決定,提高了編程的靈活性。

2.實例:

這里我們具體做一個實例來看看如何用VB.NET創建三層結構的應用程序。

數據庫:我們選擇SQL SERVER 的NorthWind數據庫。

業務層:我們創建一個WebService作為中間層。(需要安裝IIS服務)

表示層:我們寫一個Windows Form

第一步:創建WebService。

具體步驟如下:

1.新建一個項目,選擇ASP.NET Web服務,命名為:”WebService For 業務層”。

2.添加兩個Sql DataAdapter,一個為Customer_da,它指向NorthWind數據庫的Customers表,另一個為Order_da,指向Northwind數據庫的Orders表。

3.然后生成一個Typed DataSet(選擇“數據”菜單的“生成數據集”),命名為:Super_ds.

4.數據庫連接已經完成,下一步我們將考慮它與表示層之間的通信,這里我們定義兩個方法。一個為:Get_DataSet,它返回一個Super_ds類型的數據集,另一個為:Update_DataSet,它負責更新數據庫數據, 方法代碼如下:


<WebMethod()> Public Function Get_Dataset() As super_ds

customer_da.Fill(Super_ds1.Customers)

order_da.Fill(Super_ds1.Orders)

Return Super_ds1

End Function

<WebMethod()> Public Sub Update_Dataset()

Super_ds1.AcceptChanges()

End Sub


你可以運行測試一下你建立的這個WebService。它將提供兩個方法。返回的DataSet是以XML表示的。

業務層的完整代碼如下:


Imports System.Web.Services

Public Class Service1

Inherits System.Web.Services.WebService

‘Web Services Designer Generated Code…….

<WebMethod()> Public Function Get_Dataset() As super_ds

customer_da.Fill(Super_ds1.Customers)

order_da.Fill(Super_ds1.Orders)

Return Super_ds1

End Function

<WebMethod()> Public Sub Update_Dataset()

Super_ds1.AcceptChanges()

End Sub

' WEB SERVICE EXAMPLE

' The HelloWorld() example service returns the string Hello World.

' To build, uncomment the following lines then save and build the project.

' To test this web service, ensure that the .asmx file is the start page

' and press F5.

'

'<WebMethod()> Public Function HelloWorld() As String

' HelloWorld = "Hello World"

' End Function

End Class


作者:http://www.zhujiangroad.com
來源:http://www.zhujiangroad.com
北斗有巢氏 有巢氏北斗