https://www.halvorsen.
blog
Database Communication
in Visual Studio/C#
using Web Services
Hans-Petter Halvorsen
Background
• 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.
2
Software Architecture 2-Tier
3-Tier: A way to structure your code into logical parts. Different devices or
software modules can share the same code. Client-
Server
Web
3-Tier
Services
Architecture
Good Software!
We
n-Tier
bA
PI
Web Services: A standard
way to get data over a
network/Internet using
standard Web protocols
(HTTP, etc.)
APIs API: Application Programming
Interface. Different devices or software
modules can share the same code.
Code once, use it many times
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. 4
3-tier/layer Architecture
Note! The different layers
Presentation Tier PL
can be on the same
computer (Logic Layers) or
on different Computers in a
network (Physical Layers) Business Logic Tier BL
Logic Tier
Data Access Tier DAL
Data Data Tier - DL
Source
5
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
http://en.wikipedia.org/wiki/Multitier_architecture 7
3-tier/layer Architecture
Presentation 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
GUI
Application 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.
8
http://en.wikipedia.org/wiki/Multitier_architecture
3-tier Architecture
Presentation Tier Presentation Tier Presentation Tier
Business Logic Tier
Logic Tier
Different Devices can share
the same Business and Data
Data Access Tier
Access Code
Stored Procedures
The different Tiers can be
physical or logical Data Tier
Database
3-tier + WebService Architecture - Example
Team Foundation Server
Installed on one or more
Windows Servers in your LAN
or in the Cloud
TFS Cient
Web Web Presentation
Server Services Tier
Team Foundation Server
Business/Dat
a Logic Tier
Stored
Procedures
Data Data
Source Tier
10
3-tier Architecture Scenarios Client
Client
Client
Internet
Presentation Layer Presentation Layer
Presentation Layer
Firewall
Client
Client
Web Service
Presentation Layer Presentation Layer Presentation Layer
Web
Server Server
Server
Local Network (LAN)
Business Logic
Stored
Database Procedures Data Access Logic
Client
Android, iOS, Windows
Client
Client
Client WinForms 8/Windows Phone,
etc.
Presentation Tier
Mobile
Desktop App App Presentation Tier
Web App Seperate Presentation Tier
Presentation Tiers for each Device App Internet
Firewall
API Web Server Local
Presentation Tier
Web Service Network
ASP.NET Web Forms
Devices can share the same API
Business/Logic Tier and
APIs Business Tier
Logic Tier
3-tier
Clients
API Data Access Tier Architecture
API e.g., ADO, ADO.NET Scenarios
Database Data Tier Note! The different Tiers
Database can be on the same
Server
Computer (Logic Layers) or
Stored Procedures on different Computers in a
network (Physical Layers)
Tables Views
Visual Studio Projects
Solution with all Projects
(Logic Tier, Web Service,
Desktop App, Web App,
Mobile App)
Solution with Projects
used by Web App
(Logic Tier, Web App) 13
Data Tier
We are going to create the Database / Data Layer/Tier,
including:
1. Tables
2. Views
Note! Install them
3. Stored Procedures in this order
4. Triggers
5. 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
Data Tier
Triggers
Stored Procedures
Views
Data Tier
Tables
SQL Server
15
Database Tables
16
Execute the different
Scripts inside SQL Server
Management Studio
17
You are finished with the Exercise
18
Logic Tier
ASP.NET Web Forms WinForms Windows Store App
Presentation Tier Presentation Tier Presentation Tier
Purpose:
• All the Apps should/could
Logic Tier share the same Logic Tier
• To make your Apps easier
to maintain and extend
• etc.
Data Tier
Database
19
Create an Empty (Blank) Solution in Visual Studio
20
Add Project for Logic Tier (Data Access)
Select a “Class Library” Project
“LogicTier”
21
Add a New Class to the
Project (“StudentData.cs”)
“StudentData.cs” 22
Create the Code, e.g., like this (“StudentData.cs”):
Create your own Namespace A View that collects data
from several tables
Improvements: Use Try... Catch ...
23
You should test the SQL Query in the SQL Server
Management Studio first
24
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;
}
}
}
25
Create a proper name for the Assembly (.dll File)
Right-click on the Project in the Solution Explorer and select Properties
Then Build your
Project (hopefully
with no errors)
This will be the Assembly for your
Logic Tier, that can be imported
and used in other projects.
Create once – use it many times!!
26
You are finished with the Exercise
27
Presentation Layer
Desktop App: WinForms
Using Web Services (we assume the The App should be used on Internet outside the
Firewall)
Label
DataGridView
28
Step 1: Create Web Service “SchoolWS”
Create an ASP.NET Project:
“SchoolWS.asmx”
Add Web Service:
29
Web Service Code
Database ConnectionString
is located in Web.config
Web Service Method
30
Database ConnectionString is located in Web.config
31
Test Web Service
Click to Test the Web Service
Method we created
It Works!! 32
Deploy/Publish Web Service to IIS
Copy Web Service Files (Project) to default IIS Directory: C:\inetpub\wwwroot
33
34
Test if WS working:
http://localhost/SchoolWS
35
Step 2: Use Web Service in WinForm
Create New WinForm Project:
“WinFormAppWSClient”
36
Add Web Service Reference
Our Web Service Methods
37
Create GUI
Label
DataGridView
Create Code
39
WinForm Code
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()
Call the Web Service method
{
DataSet ds = new DataSet();
SchoolWSReference.SchoolWSSoapClient schoolWs = new
SchoolWSReference.SchoolWSSoapClient();
ds = schoolWs.GetStudent();
dataGridViewStudentInformation.DataSource = ds.Tables[0];
} Fill GridView
} 40
Test it:
It works!!! 41
You are finished with the Exercise
42
Hans-Petter Halvorsen
University of South-Eastern Norway
www.usn.no
E-mail: [email protected]
Web: https://www.halvorsen.blog