当前位置:首页 > 网络编程 > 软件语言 > .NET > 用VB.Net创建一个三层的数据库应用程序

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

点击次数:76 次 发布日期:2008-11-06 08:12:45 作者:源代码网
源代码网推荐
广告载入中
1. 概论:

源代码网整理以下  本文将介绍如何创建一个三层应用程序,并且将介绍如何创建一个Web Service服务。

源代码网整理以下  ADO.NET创建Windows三层结构应用程序的体系架构如下图所示:

源代码网整理以下  该结构分三个层次:表示层、业务层、数据层。

源代码网整理以下  数据层:代表物理数据库。

源代码网整理以下  业务层:负责数据层与表示层之间的数据传输。

源代码网整理以下  表示层:应用程序的客户端,它通过业务层来访问数据库。

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

源代码网整理以下  2.实例:

软件开发网 www.mscto.com

源代码网整理以下  这里我们具体做一个实例来看看如何用VB.NET创建三层结构的应用程序。

源代码网整理以下  数据库:我们选择SQL SERVER 的NorthWind数据库。

源代码网整理以下  业务层:我们创建一个WebService作为中间层。(需要安装IIS服务)

软件开发网 www.mscto.com

源代码网整理以下  表示层:我们写一个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. 软件开发网 www.mscto.com

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

软件开发网 www.mscto.com

源代码网整理以下  <WebMethod()> Public Function Get_Dataset() As super_ds

源代码网整理以下  customer_da.Fill(Super_ds1.Customers)

软件开发网 www.mscto.com

源代码网整理以下  order_da.Fill(Super_ds1.Orders)

源代码网整理以下  Return Super_ds1

源代码网整理以下  End Function

源代码网整理以下  <WebMethod()> Public Sub Update_Dataset()

源代码网整理以下  Super_ds1.AcceptChanges()

软件开发网 www.mscto.com

源代码网整理以下  End Sub

源代码网整理以下  你可以运行测试一下你建立的这个WebService.它将提供两个方法。返回的DataSet是以XML表示的。 软件开发网 www.mscto.com

源代码网整理以下  业务层的完整代码如下:

源代码网整理以下  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

软件开发网 www.mscto.com

源代码网整理以下  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 软件开发网 www.mscto.com

源代码网整理以下  " HelloWorld = "Hello World"

源代码网整理以下  " End Function

源代码网整理以下  End Class 软件开发网 www.mscto.com

源代码网整理以下  第二步:创建表示层

源代码网整理以下  具体步骤如下:

源代码网整理以下  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 "

软件开发网 www.mscto.com

源代码网整理以下  Public Sub New()

源代码网整理以下  MyBase.New()

源代码网整理以下  "This call is required by the Windows Form Designer.

源代码网整理以下  InitializeComponent() 软件开发网 www.mscto.com

源代码网整理以下  "Add any initialization after the InitializeComponent() call

源代码网整理以下  End Sub

源代码网整理以下  "Form overrides dispose to clean up the component list.

软件开发网 www.mscto.com

源代码网整理以下  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

源代码网整理以下  If disposing Then

源代码网整理以下  If Not (components Is Nothing) Then

源代码网整理以下  components.Dispose()

源代码网整理以下  End If

源代码网整理以下  End If

软件开发网 www.mscto.com

源代码网整理以下  MyBase.Dispose(disposing)

源代码网整理以下  End Sub 软件开发网 www.mscto.com

源代码网整理以下  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"

源代码网整理以下  "

软件开发网 www.mscto.com

源代码网整理以下  "Button3

源代码网整理以下  "

源代码网整理以下  Me.Button3.Location = New System.Drawing.Point(376, 360)

软件开发网 www.mscto.com

源代码网整理以下  Me.Button3.Name = "Button3"

源代码网整理以下  Me.Button3.TabIndex = 2

软件开发网 www.mscto.com

源代码网整理以下  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

软件开发网 www.mscto.com

源代码网整理以下  "

源代码网整理以下  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" 软件开发网 www.mscto.com

源代码网整理以下  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) 软件开发网 www.mscto.com

源代码网整理以下  DataGrid1.DataSource = Customer_Ds

源代码网整理以下  End Sub

源代码网整理以下  End Class

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


源代码网推荐

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华