VB.NET creates an instance of a three-layer structure

xiaoxiao2021-03-06  42

1. Introduction: This article describes how to create a three-layer application and will show you how to create a Web Service service. ADO.NET creates a Windows three-layer structure architecture architecture as shown below: This structure is divided into three levels: representing the layer, the business layer, the data layer. Data layer: represents the physical database. Business Layer: Responsible for data transmission between data layers and representations. Represents layer: Application client, which access the database through the business layer. Indicates that the layer is located in the local data that resides in memory, and when updating the database data, the update method provided by the business layer is implemented. This improves the performance of the application, and when updating the data is completely determined by you, improving the flexibility of programming. 2. Instance: Here we specifically make an example to see how to create a three-layer structure with VB.NET. Database: We choose SQL Server's Northwind database. Business Layer: We create a WebService as an intermediate layer. (Requires IIS Service) Representation: We write a Windows Form first step: Create webservice. The specific steps are as follows: 1. New project, select the ASP.NET Web service, named: "WebService for business layer". 2. Add two SQL DataAdapter, one for Customer_DA, point to the Customers table of the Northwind database, another as Order_DA, point to the ORDERS table of the Northwind database. 3. Then generate a Typed Dataset (Select the "Data" menu), named: super_ds. 4. Database connection has been completed, the next step we will consider communication between the representation, here we define Two methods. One is: get_dataset, it returns a data set of a super_ds type, another is: update_dataset, it is responsible for updating database data, the method code is as follows:

PUBLIC FUNCTION GET_DATASET () AS SUPER_DS

Customer_da.fill (super_ds1.customers)

ORDER_DA.FILL (Super_Ds1.OrDERS)

Return Super_DS1

END FUNCTION

Public sub Update_dataset ()

Super_ds1.acceptchanges ()

End Sub

You can run this WebService that you created. It will provide two ways. The returned DataSet is expressed in XML.

The full code of the business layer is as follows:

Imports System.Web.Services

Public Class Service1

Inherits System.Web.Services.Webservice

'Web Services Designer Generated Code .......

PUBLIC FUNCTION GET_DATASET () AS SUPER_DS

Customer_da.fill (super_ds1.customers)

ORDER_DA.FILL (Super_Ds1.OrDERS)

Return Super_DS1

END FUNCTION

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 the Save and Build The Project.

'Test this Web Service, EnSure That The .asmx File Is The Start Page

'and press f5.

'

'

Public function helloworld () AS STRING

'HelloWorld = "Hello World"

'End function

END CLASS

Step 2: Create a representation layer

Specific steps are as follows:

1. Create a Windows application, named: "Windows Form for Layer".

2. Add a DataGrid on the form, a button, button1's text is "LOAD", the function is: read data from the business layer.

3. Add a web reference in the Solution Form to introduce our own Web Service for business layer into the current project.

4. Add the following code to Button1 Click event:

DIM CUSTOMER_DS As New LocalHost.super_ds ()

Dim ser1 as new localhost.service1 ()

Customer_ds.merge (ser1.get_dataset)

DataGrid1.datasource = Customer_DS

Here we call the web service's get_dataset function, the call of the Update_DataSet method is exactly the same.

The complete code indicating the layer is as follows:

Imports data_access_ represents a layer

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 anyinitization 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_Gerate layer .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.

Private subinitializeComponent ()

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_ represents layer .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 (byvale 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

Summary: It can be seen, indicating that the layer form does not have a database connection control, which is completed by the connection task of the database, so that the structure of the program is more clear, and the implementation of the business layer can also be used, for example: Write a class to complete data transfer with the database.

转载请注明原文地址:https://www.9cbs.com/read-58236.html

New Post(0)