本文将介绍如何创建一个三层应用程序,并且将介绍如何创建一个WebService服务。
ADO.NET创建Windows三层结构应用程序的体系架构如下图所示:
该结构分三个层次:表示层、业务层、数据层。
数据层:代表物理数据库。
业务层:负责数据层与表示层之间的数据传输。
表示层:应用程序的客户端,它通过业务层来访问数据库。
表示层所操作的是驻留在内存中的本地数据,当需要更新数据库数据时,要通过业务层提供的更新方法实现。这样可以大大提高应用程序的性能,而且,什么时候更新数据完全由你决定,提高了编程的灵活性。
2.实例:
这里我们具体做一个实例来看看如何用VB.NET创建三层结构的应用程序。
财管家.园.fs119.net
数据库:我们选择SQLSERVER的NorthWind数据库。
业务层:我们创建一个WebService作为中间层。(需要安装IIS服务)
表示层:我们写一个WindowsForm
财软联 盟 fs119.net
财管家园.fs119.net
财管,家园,fs119.net
第一步:创建WebService。
具体步骤如下:
1.新建一个项目,选择ASP.NETWeb服务,命名为:”WebServiceFor业务层”。
2.添加两个SqlDataAdapter,一个为Customer_da,它指向NorthWind数据库的Customers表,另一个为Order_da,指向Northwind数据库的Orders表。
3.然后生成一个TypedDataSet(选择“数据”菜单的“生成数据集”),命名为:Super_ds.
4.数据库连接已经完成,下一步我们将考虑它与表示层之间的通信,这里我们定义两个方法。一个为:Get_DataSet,它返回一个Super_ds类型的数据集,另一个为:Update_DataSet,它负责更新数据库数据,方法代码如下:
<WebMethod()>PublicFunctionGet_Dataset()Assuper_ds
customer_da.Fill(Super_ds1.Customers)
order_da.Fill(Super_ds1.Orders)
ReturnSuper_ds1
EndFunction
<WebMethod()>PublicSubUpdate_Dataset()
Super_ds1.AcceptChanges()
EndSub
你可以运行测试一下你建立的这个WebService。它将提供两个方法。返回的DataSet是以XML表示的。
业务层的完整代码如下:
ImportsSystem.Web.Services
PublicClassService1
InheritsSystem.Web.Services.WebService
‘WebServicesDesignerGeneratedCode…….
<WebMethod()>PublicFunctionGet_Dataset()Assuper_ds
customer_da.Fill(Super_ds1.Customers) 财管.家园.fs119.net
order_da.Fill(Super_ds1.Orders)
ReturnSuper_ds1
EndFunction
<WebMethod()>PublicSubUpdate_Dataset()
Super_ds1.AcceptChanges()
EndSub
'WEBSERVICEEXAMPLE
'TheHelloWorld()exampleservicereturnsthestringHelloWorld.
'Tobuild,uncommentthefollowinglinesthensaveandbuildtheproject.
'Totestthiswebservice,ensurethatthe.asmxfileisthestartpage
'andpressF5.
'
'<WebMethod()>PublicFunctionHelloWorld()AsString
'HelloWorld="HelloWorld"
'EndFunction
EndClass
财 管家园 fs119.net
财管家,园,fs119.net
财软联盟 fs119.net财软,联盟,fs119.net
具体步骤如下:
1.新建一个Windows应用程序,命名为:“WindowsFormFor表示层”。
2.在窗体上添加一个DataGrid,一个Button,Button1的text为“Load”,作用是:从业务层读取数据。
3.在解决方案窗体中添加Web引用,将我们自己建立的WebServicefor业务层引入到当前项目中。
4.向Button1的Click事件添加如下代码:
DimCustomer_DsAsNewlocalhost.super_ds()
Dimser1AsNewlocalhost.Service1()
Customer_Ds.Merge(ser1.Get_Dataset)
DataGrid1.DataSource=Customer_Ds
这里我们调用了WebService的Get_DataSet函数,Update_DataSet方法的调用与此完全相同。
表示层的完整代码如下:
ImportsData_Access_表示层
PublicClassForm1
InheritsSystem.Windows.Forms.Form
#Region"WindowsFormDesignergeneratedcode"
PublicSubNew()
MyBase.New()
'ThiscallisrequiredbytheWindowsFormDesigner.
InitializeComponent()
'AddanyinitializationaftertheInitializeComponent()call
EndSub 财管家 园 fs119.net
'Formoverridesdisposetocleanupthecomponentlist.
ProtectedOverloadsOverridesSubDispose(ByValdisposingAsBoolean)
IfdisposingThen
IfNot(componentsIsNothing)Then
components.Dispose()
EndIf
EndIf
MyBase.Dispose(disposing)
EndSub
FriendWithEventsButton1AsSystem.Windows.Forms.Button
FriendWithEventsButton2AsSystem.Windows.Forms.Button
FriendWithEventsButton3AsSystem.Windows.Forms.Button
FriendWithEventsClient_DataSetAsData_Access_表示层.localhost.super_ds
FriendWithEventsDataGrid1AsSystem.Windows.Forms.DataGrid
'RequiredbytheWindowsFormDesigner
PrivatecomponentsAsSystem.ComponentModel.Container
'NOTE:ThefollowingprocedureisrequiredbytheWindowsFormDesigner
'ItcanbemodifiedusingtheWindowsFormDesigner.
'Donotmodifyitusingthecodeeditor.
<System.Diagnostics.DebuggerStepThrough()>PrivateSubInitializeComponent()
Me.Button1=NewSystem.Windows.Forms.Button()
Me.Button2=NewSystem.Windows.Forms.Button()
Me.Button3=NewSystem.Windows.Forms.Button()
Me.Client_DataSet=NewData_Access_表示层.localhost.super_ds()
Me.DataGrid1=NewSystem.Windows.Forms.DataGrid()
CType(Me.Client_DataSet,System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.DataGrid1,System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location=NewSystem.Drawing.Point(88,360)
Me.Button1.Name="Button1"
Me.Button1.TabIndex=0
Me.Button1.Text="load"
财 软联盟 fs119.net
'
'Button2
'
Me.Button2.Location=NewSystem.Drawing.Point(232,360)
Me.Button2.Name="Button2"
Me.Button2.TabIndex=1
Me.Button2.Text="update"
'
'Button3
'
Me.Button3.Location=NewSystem.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=NewSystem.Globalization.CultureInfo("zh-CN")
Me.Client_DataSet.Namespace="http://www.tempuri.org/CustomerDs.xsd"
'
'DataGrid1
'
Me.DataGrid1.DataMember=""
Me.DataGrid1.Location=NewSystem.Drawing.Point(40,56)
财软联盟.fs119.net
Me.DataGrid1.Name="DataGrid1"
Me.DataGrid1.Size=NewSystem.Drawing.Size(480,264)
Me.DataGrid1.TabIndex=3
'
'Form1
'
Me.AutoScaleBaseSize=NewSystem.Drawing.Size(6,14)
Me.ClientSize=NewSystem.Drawing.Size(568,429)
Me.Controls.AddRange(NewSystem.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)
EndSub
#EndRegion
PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click
DimCustomer_DsAsNewlocalhost.super_ds()
财软,联盟,fs119.net
Dimser1AsNewlocalhost.Service1()
Customer_Ds.Merge(ser1.Get_Dataset)
DataGrid1.DataSource=Customer_Ds
EndSub
EndClass
总结:可见,表示层窗体上完全没有数据库连接控件,它与数据库的连接任务是通过业务层来完成的,这样,程序的结构更加清晰,当然业务层的实现也可以用其他方法,比如:写一个自己的类来完成与数据库的数据传输。
财.软联盟.fs119.net
财软.联盟.fs119.net
Google.cn搜索相关文章:
谷歌中搜索全球网 如何用VB.Net创建一个三层的数据库应用程序
百度中搜索 如何用VB.Net创建一个三层的数据库应用程序
谷歌中搜索www.fs119.net 如何用VB.Net创建一个三层的数据库应用程序
下一篇:一步一步创建VisualBasic.NET控件
精品课程推荐
- 在VisualBasic.NET中实现后台进程(三
- 在VisualBasic.NET中实现后台进程(二
- 在VisualBasic.NET中实现后台进程(一
- VB.NET开发互联网应用
- VisualBasic.NET中操作MsAgent
- VisualBasic.NET快速开发MIS系统
- VisualBasic.NET实现后台处理
- 运用VB.net创建Web服务访问程序
- 一步一步创建VisualBasic.NET控件
- 如何用VB.Net创建一个三层的数据库应
- VisualBasic.NET中动态加载类
- VisualBasic.NET中访问数据的方法
- 消息队列在VB.NET数据库开发中的应用
- VB.NET窗体操作技巧两则
- VB.NET中的多线程开发