How to use VB. Net to create a three-tier Database Application
Source: Internet
Author: User
1. Overview:
This article describes how to create a layer-3 Application and how to create a web service.
Shows the architecture for creating a Windows L3 application using ADO. Net:
The structure consists of three layers: presentation layer, business layer, and data layer.
Data Layer: represents a physical database.
Business Layer: responsible for data transmission between the data layer and the presentation layer.
Presentation layer: the client of the application, which accesses the database through the business layer.
The presentation layer operates on local data residing in the memory. To update database data, you must use the update method provided by the business layer. This can greatly improve the performance of the application, and it is up to you to decide when to update the data, improving programming flexibility.
2. instance:
Here we will create an example to see how to use VB. NET to create a layer-3 application.
Database: select the SQL server's northwind database.
Business Layer: we create a WebService as the middle layer. (IIS service needs to be installed)
Presentation Layer: we write a Windows form
Step 1: Create a WebService.
The procedure is as follows:
1. Create a new project, select ASP. NET web service, and name it "WebService for business layer ".
2. Add two SQL dataadapters, Customer_da, pointing to the northmers table of the NorthWind database, and Order_da, pointing to the Orders table of the Northwind database.
3. Generate a Typed DataSet (select "generate DataSet" in the "data" menu) named Super_ds.
4. The database connection has been completed. Next we will consider the communication between the database and the presentation layer. Here we define two methods. One is Get_DataSet, which returns a Super_ds type dataset and the other is Update_DataSet. It is responsible for updating database data. The method code is as follows:
<WebMethod ()> Public Function Get_Dataset () As super_ds
Customer_da.Fill (super_ds1.mers mers)
Order_da.Fill (Super_ds1.Orders)
Return Super_ds1
End Function
<WebMethod ()> Public Sub Update_Dataset ()
Super_ds1.AcceptChanges ()
End Sub
You can run and test the WebService you created. It provides two methods. The returned DataSet is expressed in XML.
The complete code for the business layer is as follows:
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.mers mers)
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
Step 2: Create a presentation layer
The procedure is as follows:
1. Create a new Windows application named "Windows Form For presentation layer ".
2. Add a DataGrid, a Button, and the text of Button1 is "Load" to read data from the business layer.
3. Add a Web reference in the solution form and introduce the Web Service for Service layer we have created to the current project.
4. Add the following code to the Click event of Button1:
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 Get_DataSet function of Web Service. The call of the Update_DataSet method is exactly the same.
The complete code for the presentation layer is as follows:
Imports Data_Access _ presentation 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 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 _ presentation 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.
<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 _ presentation 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"
'
'Create1'
'
Me. datagrid1.datamember = ""
Me. datagrid1.location = new system. Drawing. Point (40, 56)
Me. Maid = "maid"
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 button#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
Summary: it can be seen that there is no database connection control on the presentation layer form, and its connection tasks to the database are completed through the business layer. In this way, the program structure is clearer, of course, you can also use other methods to implement the business layer, such as writing a class of your own to complete data transmission with the database.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.