Database Communication Using Web Services
Database Communication Using Web Services
blog
Database Communication
in Visual Studio/C#
using Web Services
Hans-Petter Halvorsen
Background
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.
Logic Tier
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
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
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
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
4. Triggers
5. Script for some “Dummy” Data
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
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”):
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";
return ds;
}
}
}
25
Create a proper name for the Assembly (.dll File)
Right-click on the Project in the Solution Explorer and select Properties
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
31
Test Web Service
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
37
Create GUI
Label
DataGridView
Create Code
39
WinForm Code
using System.Windows.Forms;
namespace WinFormAppWSClient
{
public partial class FormWSClient : Form
{
public FormWSClient()
{
InitializeComponent();
}
FillStudentGrid();
}
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