国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

如何用VB.Net創建一個三層的數據庫應用程序(轉貼

2024-07-10 13:01:20
字體:
來源:轉載
供稿:網友

商業源碼熱門下載www.html.org.cn

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

第二步:創建表示層

具體步驟如下:

1. 新建一個windows應用程序,命名為:“windows form for 表示層”。

2. 在窗體上添加一個datagrid,一個button,button1的text為“load”,作用是:從業務層讀取數據。

3. 在解決方案窗體中添加web 引用,將我們自己建立的web service for 業務層引入到當前項目中。

4. 向button1的click事件添加如下代碼:

dim customer_ds as new localhost.super_ds()

dim ser1 as new localhost.service1()

customer_ds.merge(ser1.get_dataset)

datagrid1.datasource = customer_ds

這里我們調用了web service的get_dataset函數,update_dataset方法的調用與此完全相同。

表示層的完整代碼如下:

imports data_access_表示層

public class form1

inherits system.windows.forms.form

#region " windows form designer generated code "

public sub new()

mybase.new()

'this call is required by the windows form designer.

initializecomponent()

'add any initialization after the initializecomponent() call

end sub

'form overrides dispose to clean up the component list.

protected overloads overrides sub dispose(byval disposing as boolean)

if disposing then

if not (components is nothing) then

components.dispose()

end if

end if

mybase.dispose(disposing)

end sub

friend withevents button1 as system.windows.forms.button

friend withevents button2 as system.windows.forms.button

friend withevents button3 as system.windows.forms.button

friend withevents client_dataset as data_access_表示層.localhost.super_ds

friend withevents datagrid1 as system.windows.forms.datagrid

'required by the windows form designer

private components as system.componentmodel.container

'note: the following procedure is required by the windows form designer

'it can be modified using the windows form designer.

'do not modify it using the code editor.

<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()

me.button1 = new system.windows.forms.button()

me.button2 = new system.windows.forms.button()

me.button3 = new system.windows.forms.button()

me.client_dataset = new data_access_表示層.localhost.super_ds()

me.datagrid1 = new system.windows.forms.datagrid()

ctype(me.client_dataset, system.componentmodel.isupportinitialize).begininit()

ctype(me.datagrid1, system.componentmodel.isupportinitialize).begininit()

me.suspendlayout()

'

'button1

'

me.button1.location = new system.drawing.point(88, 360)

me.button1.name = "button1"

me.button1.tabindex = 0

me.button1.text = "load"

'

'button2

'

me.button2.location = new system.drawing.point(232, 360)

me.button2.name = "button2"

me.button2.tabindex = 1

me.button2.text = "update"

'

'button3

'

me.button3.location = new system.drawing.point(376, 360)

me.button3.name = "button3"

me.button3.tabindex = 2

me.button3.text = "clear"

'

'client_dataset

'

me.client_dataset.datasetname = "client_dataset"

me.client_dataset.locale = new system.globalization.cultureinfo("zh-cn")

me.client_dataset.namespace = "http://www.tempuri.org/customerds.xsd"

'

'datagrid1

'

me.datagrid1.datamember = ""

me.datagrid1.location = new system.drawing.point(40, 56)

me.datagrid1.name = "datagrid1"

me.datagrid1.size = new system.drawing.size(480, 264)

me.datagrid1.tabindex = 3

'

'form1

'

me.autoscalebasesize = new system.drawing.size(6, 14)

me.clientsize = new system.drawing.size(568, 429)

me.controls.addrange(new system.windows.forms.control() {me.datagrid1, me.button3, me.button2, me.button1})

me.name = "form1"

me.text = "form1"

ctype(me.client_dataset, system.componentmodel.isupportinitialize).endinit()

ctype(me.datagrid1, system.componentmodel.isupportinitialize).endinit()

me.resumelayout(false)

end sub

#end region

private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click

dim customer_ds as new localhost.super_ds()

dim ser1 as new localhost.service1()

customer_ds.merge(ser1.get_dataset)

datagrid1.datasource = customer_ds

end sub

end class

總結:可見,表示層窗體上完全沒有數據庫連接控件,它與數據庫的連接任務是通過業務層來完成的,這樣,程序的結構更加清晰,當然業務層的實現也可以用其他方法,比如:寫一個自己的類來完成與數據庫的數據傳輸
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天等县| 红原县| 峡江县| 海阳市| 天气| 西峡县| 汤阴县| 周至县| 亳州市| 昭觉县| 德钦县| 陇西县| 大方县| 策勒县| 游戏| 新巴尔虎右旗| 阿拉尔市| 邢台县| 武汉市| 鄂托克旗| 社会| 大同市| 阳东县| 皋兰县| 镶黄旗| 托克逊县| 容城县| 简阳市| 民权县| 许昌市| 兴宁市| 太康县| 堆龙德庆县| 门头沟区| 乌审旗| 长沙县| 定西市| 科技| 渭南市| 兰州市| 海丰县|