Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you...
Transcript of Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you...
![Page 1: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/1.jpg)
Database Communication in Visual Studio/C# using Web Services
https://www.halvorsen.blog
Hans-Petter Halvorsen
![Page 2: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/2.jpg)
Background
2
• With Web Services you can easily get your data through Internet
• We will use Web Services because we assume that the the App should be used on Internet outside the Firewall).
• The Database is located on a Server that has no direct access to the Internet.
![Page 3: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/3.jpg)
Client-Server
Software Architecture
Web Services
APIs
3-Tier Architecture
API: Application Programming Interface. Different devices or software modules can share the same code. Code once, use it many times
Web Services: A standard way to get data over a network/Internet using standard Web protocols (HTTP, etc.)
3-Tier: A way to structure your code into logical parts. Different devices or software modules can share the same code.
Good Software!
2-Tier
n-Tier
Web API
![Page 4: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/4.jpg)
4
The database-centric style. Typically, the clients communicate directly with the database.
A three-tier style, in which clients do not connect directly to the database.
Web Services, etc.
![Page 5: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/5.jpg)
3-tier/layer Architecture
5
Presentation Tier
Business Logic Tier
Data Access Tier
Data Source
PL
BL
DAL
Note! The different layers can be on the same computer (Logic Layers) or on different Computers in a network (Physical Layers)
Data Tier - DL
Logic Tier
![Page 6: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/6.jpg)
Why 3-Tier (N-Tier Architecture?)• Flexible applications• Reusable code– Code once, use many times
• Modularized– You need only to change part of the code– You can deploy only one part– You can Test only one part– Multiple Developers
• Different parts (Tiers) can be stored on different computers
• Different Platforms and Languages can be used• etc.
6
![Page 7: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/7.jpg)
7http://en.wikipedia.org/wiki/Multitier_architecture
![Page 8: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/8.jpg)
3-tier/layer ArchitecturePresentation Tier• This is the topmost level of the application. • The presentation tier displays information related to such services as browsing merchandise, purchasing
and shopping cart contents. • It communicates with other tiers by which it puts out the results to the browser/client tier and all other
tiers in the network. • In simple terms it is a layer which users can access directly such as a web page, or an operating systems
GUIApplication tier (business logic, logic tier, data access tier, or middle tier)• The logical tier is pulled out from the presentation tier and, as its own layer. • It controls an application’s functionality by performing detailed processing.Data tier• This tier consists of database servers. Here information is stored and retrieved. • This tier keeps data neutral and independent from application servers or business logic. • Giving data its own tier also improves scalability and performance.
8http://en.wikipedia.org/wiki/Multitier_architecture
![Page 9: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/9.jpg)
3-tier Architecture
Presentation Tier
Business Logic Tier
Data Access Tier
Database
Presentation Tier Presentation Tier
Stored Procedures
Different Devices can share the same Business and Data Access Code
The different Tiers can be physical or logical Data Tier
Logic Tier
![Page 10: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/10.jpg)
Web Server
3-tier + WebService Architecture - Example
10
Presentation Tier
Business/Data Logic Tier
Data Source
Web Services
Team
Foun
datio
n Se
rver
TFS Cient
Installed on one or more Windows Servers in your LAN or in the Cloud
Data Tier
Stored Procedures
Team Foundation Server
![Page 11: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/11.jpg)
3-tier Architecture Scenarios
Presentation Layer
Business Logic
Data Access LogicDatabase
Presentation Layer Presentation Layer
Stored Procedures
Client
ClientClient
Web Server
Web Service
Presentation Layer
Client
Presentation Layer
Internet
Local Network (LAN)
Client
Firewall
Presentation Layer
ServerServer
![Page 12: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/12.jpg)
Database
Presentation Tier
e.g., ADO, ADO.NET
Logic Tier
Web Service
Business Tier
Data Access Tier
Data Tier
Stored ProceduresViewsTables
Web Server
DatabaseServer
Presentation Tier
Client
ASP.NET Web Forms
Web AppPresentation Tier
Client
Clie
nt
ClientWinForms
3-tier Architecture
Scenarios
Firewall
Clients
Desktop AppMobile
App
Internet
Local Network
Note! The different Tiers can be on the same Computer (Logic Layers) or on different Computers in a network (Physical Layers)
Android, iOS, Windows 8/Windows Phone,
etc.
Presentation Tiers
Devices can share the same Business/Logic Tier and APIs
Seperate Presentation Tier for each Device App
API
API
API
API
![Page 13: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/13.jpg)
13
Visual Studio ProjectsSolution with all Projects (Logic Tier, Web Service, Desktop App, Web App, Mobile App)
Solution with Projects used by Web App (Logic Tier, Web App)
![Page 14: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/14.jpg)
Data TierWe are going to create the Database / Data Layer/Tier, including:1. Tables2. Views3. Stored Procedures4. Triggers5. Script for some “Dummy” Data
Download Zip Files with Tables, Views, Stored Procedures and Triggerse in order to create the Data Tier in SQL Server (The ZIP File is located on the same place as this File)
14
Note! Install them in this order
![Page 15: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/15.jpg)
Data Tier
15
Tables
Views
Stored Procedures
SQL Server
Triggers
Data Tier
![Page 16: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/16.jpg)
16
Database Tables
![Page 17: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/17.jpg)
17
Execute the different Scripts inside SQL Server
Management Studio
![Page 18: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/18.jpg)
18You are finished with the Exercise
![Page 19: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/19.jpg)
Logic Tier
19
Database
Presentation Tier
Logic Tier
ASP.NET Web Forms
Data Tier
Presentation Tier Presentation Tier
WinForms Windows Store App
Purpose: • All the Apps should/could
share the same Logic Tier• To make your Apps easier
to maintain and extend• etc.
![Page 20: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/20.jpg)
20
Create an Empty (Blank) Solution in Visual Studio
![Page 21: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/21.jpg)
21
Add Project for Logic Tier (Data Access)
Select a “Class Library” Project
“LogicTier”
![Page 22: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/22.jpg)
22
Add a New Class to the Project (“StudentData.cs”)
“StudentData.cs”
![Page 23: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/23.jpg)
23
Create the Code, e.g., like this (“StudentData.cs”):
Create your own Namespace
Improvements: Use Try... Catch ...
A View that collects data from several tables
![Page 24: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/24.jpg)
24
You should test the SQL Query in the SQL Server Management Studio first
![Page 25: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/25.jpg)
25
Code (“StudentData.cs”):using System.Data.SqlClient;using System.Data.SqlTypes;using System.Data;
namespace Tuc.School.LogicTier{
public class StudentData{
public DataSet GetStudentDB(string connectionString){
string selectSQL = "select StudentName, StudentNumber, SchoolName, ClassName, Grade from StudentData order by StudentName";
// Define the ADO.NET objects.SqlConnection con = new SqlConnection(connectionString);
SqlDataAdapter da = new SqlDataAdapter(selectSQL, con);
DataSet ds = new DataSet();da.Fill(ds);
return ds;
}}
}
![Page 26: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/26.jpg)
26
Create a proper name for the Assembly (.dll File)
This will be the Assembly for your Logic Tier, that can be imported and used in other projects.Create once – use it many times!!
Then Build your Project (hopefully with no errors)
Right-click on the Project in the Solution Explorer and select Properties
![Page 27: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/27.jpg)
27You are finished with the Exercise
![Page 28: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/28.jpg)
28
Presentation LayerDesktop App: WinForms
Using Web Services (we assume the The App should be used on Internet outside the Firewall)
Label
DataGridView
![Page 29: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/29.jpg)
29
Step 1: Create Web Service
Add Web Service:
Create an ASP.NET Project:“SchoolWS”
“SchoolWS.asmx”
![Page 30: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/30.jpg)
30
Web Service Code
Database ConnectionStringis located in Web.config
Web Service Method
![Page 31: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/31.jpg)
31
Database ConnectionString is located in Web.config
![Page 32: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/32.jpg)
32
Test Web Service
It Works!!
Click to Test the Web Service Method we created
![Page 33: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/33.jpg)
33
Deploy/Publish Web Service to IISCopy Web Service Files (Project) to default IIS Directory: C:\inetpub\wwwroot
![Page 34: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/34.jpg)
34
![Page 35: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/35.jpg)
35
Test if WS working:
http://localhost/SchoolWS
![Page 36: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/36.jpg)
36
Step 2: Use Web Service in WinFormCreate New WinForm Project:
“WinFormAppWSClient”
![Page 37: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/37.jpg)
37
Add Web Service Reference
Our Web Service Methods
![Page 38: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/38.jpg)
Create GUI
Label
DataGridView
![Page 39: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/39.jpg)
39
Create Code
![Page 40: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/40.jpg)
40
using System.Windows.Forms;
namespace WinFormAppWSClient{
public partial class FormWSClient : Form{
public FormWSClient(){
InitializeComponent();}
private void FormWSClient_Load(object sender, EventArgs e){
FillStudentGrid();}
private void FillStudentGrid(){
DataSet ds = new DataSet();
SchoolWSReference.SchoolWSSoapClient schoolWs = newSchoolWSReference.SchoolWSSoapClient();
ds = schoolWs.GetStudent();
dataGridViewStudentInformation.DataSource = ds.Tables[0];
}}
WinForm Code
Call the Web Service method
Fill GridView
![Page 41: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/41.jpg)
41
Test it:
It works!!!
![Page 42: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/42.jpg)
42You are finished with the Exercise
![Page 43: Database Communication using Web Services · 2018-06-28 · Background 2 • With Web Services you can easily get your data through Internet • We will use Web Services because we](https://reader036.fdocuments.us/reader036/viewer/2022070622/5e46e673ecb15132212e6c45/html5/thumbnails/43.jpg)
Hans-Petter Halvorsen
University of South-Eastern Norwaywww.usn.no
E-mail: [email protected]: https://www.halvorsen.blog