vb.net

位置:IT落伍者 >> vb.net >> 浏览文章

用VB.Net创建一个三层的数据库应用程序


发布日期:2023年06月18日
 
用VB.Net创建一个三层的数据库应用程序

概论

本文将介绍如何创建一个三层应用程序并且将介绍如何创建一个Web Service服务

ADONET创建Windows三层结构应用程序的体系架构如下图所示

该结构分三个层次表示层业务层数据层

数据层代表物理数据库

业务层负责数据层与表示层之间的数据传输

表示层应用程序的客户端它通过业务层来访问数据库

表示层所操作的是驻留在内存中的本地数据当需要更新数据库数据时要通过业务层提供的更新方法实现这样可以大大提高应用程序的性能而且什么时候更新数据完全由你决定提高了编程的灵活性

实例

这里我们具体做一个实例来看看如何用VBNET创建三层结构的应用程序

数据库我们选择SQL SERVER 的NorthWind数据库

业务层我们创建一个WebService作为中间层(需要安装IIS服务)

表示层我们写一个Windows Form

第一步创建WebService

具体步骤如下

新建一个项目选择ASPNET Web服务命名为WebService For 业务层

添加两个Sql DataAdapter一个为Customer_da它指向NorthWind数据库的Customers表另一个为Order_da指向Northwind数据库的Orders表

然后生成一个Typed DataSet(选择数据菜单的生成数据集命名为Super_ds

数据库连接已经完成下一步我们将考虑它与表示层之间的通信这里我们定义两个方法一个为Get_DataSet它返回一个Super_ds类型的数据集另一个为Update_DataSet它负责更新数据库数据 方法代码如下

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

customer_daFill(Super_dsCustomers)

order_daFill(Super_dsOrders)

Return Super_ds

End Function

<WebMethod()> Public Sub Update_Dataset()

Super_dsAcceptChanges()

End Sub

你可以运行测试一下你建立的这个WebService它将提供两个方法返回的DataSet是以XML表示的

业务层的完整代码如下

Imports SystemWebServices

Public Class Service

Inherits SystemWebServicesWebService

Web Services Designer Generated Code……

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

customer_daFill(Super_dsCustomers)

order_daFill(Super_dsOrders)

Return Super_ds

End Function

<WebMethod()> Public Sub Update_Dataset()

Super_dsAcceptChanges()

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 F

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

HelloWorld = Hello World

End Function

End Class

第二步创建表示层

具体步骤如下

新建一个Windows应用程序命名为Windows Form For 表示层

在窗体上添加一个DataGrid一个ButtonButton的text为Load作用是从业务层读取数据

在解决方案窗体中添加Web 引用将我们自己建立的Web Service for 业务层引入到当前项目中

向Button的Click事件添加如下代码

Dim Customer_Ds As New localhostsuper_ds()

Dim ser As New localhostService()

Customer_DsMerge(serGet_Dataset)

DataGridDataSource = Customer_Ds

这里我们调用了Web Service的Get_DataSet函数Update_DataSet方法的调用与此完全相同

表示层的完整代码如下

Imports Data_Access_表示层

Public Class Form

Inherits SystemWindowsFormsForm

#Region Windows Form Designer generated code

Public Sub New()

MyBaseNew()

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

componentsDispose()

End If

End If

MyBaseDispose(disposing)

End Sub

Friend WithEvents Button As SystemWindowsFormsButton

Friend WithEvents Button As SystemWindowsFormsButton

Friend WithEvents Button As SystemWindowsFormsButton

Friend WithEvents Client_DataSet As Data_Access_表示层localhostsuper_ds

Friend WithEvents DataGrid As SystemWindowsFormsDataGrid

Required by the Windows Form Designer

Private components As SystemComponentModelContainer

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

<SystemDiagnosticsDebuggerStepThrough()> Private Sub InitializeComponent()

MeButton = New SystemWindowsFormsButton()

MeButton = New SystemWindowsFormsButton()

MeButton = New SystemWindowsFormsButton()

MeClient_DataSet = New Data_Access_表示层localhostsuper_ds()

MeDataGrid = New SystemWindowsFormsDataGrid()

CType(MeClient_DataSet SystemComponentModelISupportInitialize)BeginInit()

CType(MeDataGrid SystemComponentModelISupportInitialize)BeginInit()

MeSuspendLayout()

Button

MeButtonLocation = New SystemDrawingPoint(

MeButtonName = Button

MeButtonTabIndex =

MeButtonText = load

Button

MeButtonLocation = New SystemDrawingPoint(

MeButtonName = Button

MeButtonTabIndex =

MeButtonText = update

Button

MeButtonLocation = New SystemDrawingPoint(

MeButtonName = Button

MeButtonTabIndex =

MeButtonText = clear

Client_DataSet

MeClient_DataSetDataSetName = Client_DataSet

MeClient_DataSetLocale = New SystemGlobalizationCultureInfo(zhCN

MeClient_DataSetNamespace =

DataGrid

MeDataGridDataMember =

MeDataGridLocation = New SystemDrawingPoint(

MeDataGridName = DataGrid

MeDataGridSize = New SystemDrawingSize(

MeDataGridTabIndex =

Form

MeAutoScaleBaseSize = New SystemDrawingSize(

MeClientSize = New SystemDrawingSize(

MeControlsAddRange(New SystemWindowsFormsControl() {MeDataGrid MeButton MeButton MeButton})

MeName = Form

MeText = Form

CType(MeClient_DataSet SystemComponentModelISupportInitialize)EndInit()

CType(MeDataGrid SystemComponentModelISupportInitialize)EndInit()

MeResumeLayout(False)

End Sub

#End Region

Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles ButtonClick

Dim Customer_Ds As New localhostsuper_ds()

Dim ser As New localhostService()

Customer_DsMerge(serGet_Dataset)

DataGridDataSource = Customer_Ds

End Sub

End Class

总结可见表示层窗体上完全没有数据库连接控件它与数据库的连接任务是通过业务层来完成的这样程序的结构更加清晰当然业务层的实现也可以用其他方法比如写一个自己的类来完成与数据库的数据传输

上一篇:一步一步在VB.NET中使用抽象类

下一篇:浅析VB.NET局部静态变量