[Repost] How to create a three-story database application with VB.NET

xiaoxiao2021-03-06  65

Author: rcs

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 sauer_ds type dataset, the other 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 the test you created this WebService. It will provide two ways. The returned DataSet is expressed in XML.

Complete code business layer 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 then save and build the project. 'To 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: Creating a Layer Specific steps are as follows: 1. Create a new 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 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 Web Service The function of the update_dataset method is exactly the same.

Represents a complete code layers are 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 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_ 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 = "loading"' 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.dataMem Ber = "" 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"

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

New Post(0)