选择显示字体大小

如何用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
  
  第二步:创建表示层
  
  具体步骤如下:
  
  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
  
  总结:可见,表示层窗体上完全没有数据库连接控件,它与数据库的连接任务是通过业务层来完成的,这样,程序的结构更加清晰,当然业务层的实现也可以用其他方法,比如:写一个自己的类来完成与数据库的数据传输。
  
  (转自51do.net)  


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons