database communication in c# using 3 layer architecture
TRANSCRIPT
![Page 1: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/1.jpg)
Database Communica/on in Visual Studio/C#
using 3-‐/er Arcitecture
Hans-‐Pe=er Halvorsen, M.Sc.
The examples uses ADO.NET
![Page 2: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/2.jpg)
2
The database-‐centric style. Typically, the clients communicate directly with the database.
A three-‐/er style, in which clients do not connect directly to the database.
Web Services, etc.
![Page 3: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/3.jpg)
3-‐/er/layer Architecture
3
Presenta/on Tier
Business Logic Tier
Data Access Tier
Data Source
PL
BL
DAL
Note! The
differen
t layers c
an be on
the
same compu
ter (Logic Layers) o
r on diffe
rent
Compu
ters in a network (Physic
al Layers)
Data Tier -‐ DL
Logic Tier
![Page 4: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/4.jpg)
Why 3-‐Tier (N-‐Tier Architecture?) • Flexible applica/ons • Reusable code – Code once, use many /mes
• Modularized – You need only to change part of the code – You can deploy only one part – You can Test only one part – Mul/ple Developers
• Different parts (Tiers) can be stored on different computers
• Different PlaYorms and Languages can be used • etc.
4
![Page 5: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/5.jpg)
5 h=p://en.wikipedia.org/wiki/Mul//er_architecture
![Page 6: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/6.jpg)
3-‐/er/layer Architecture Presenta4on Tier • This is the topmost level of the applica/on. • The presenta/on /er displays informa/on related to such services as browsing
merchandise, purchasing and shopping cart contents. • It communicates with other /ers by which it puts out the results to the
browser/client /er and all other /ers in the network. • In simple terms it is a layer which users can access directly such as a web page,
or an opera/ng systems GUI Applica4on 4er (business logic, logic 4er, data access 4er, or middle 4er) • The logical /er is pulled out from the presenta/on /er and, as its own layer. • It controls an applica/on’s func/onality by performing detailed processing. Data 4er • This /er consists of database servers. Here informa/on is stored and retrieved. • This /er keeps data neutral and independent from applica/on servers or
business logic. • Giving data its own /er also improves scalability and performance.
6 h=p://en.wikipedia.org/wiki/Mul//er_architecture
![Page 7: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/7.jpg)
3-‐/er Architecture
Presenta/on Tier
Business Logic Tier
Data Access Tier
Database
Presenta/on Tier Presenta/on 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 8: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/8.jpg)
Web Server
3-‐/er + WebService Architecture -‐ Example
8
Presenta/on Tier
Business/Data Logic
Tier
Data Source
Web Services
Team
Fou
nda/
on Server
TFS Cient
Installed on one or more Windows Servers in your LAN or in the Cloud
Data Tier
Stored Procedures
Team Founda/on Server
![Page 9: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/9.jpg)
3-‐/er Architecture Scenarios
Presenta/on Layer
Business Logic
Data Access Logic Database
Presenta/on Layer Presenta/on Layer
Stored Procedures
Client
Client Client
Web Server
Web Service
Presenta/on Layer
Client
Presenta/on Layer
Internet
Local Network (LAN)
Client
Firewall
Presenta/on Layer
Server Server
![Page 10: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/10.jpg)
Crea/ng the Data Tier
Hans-‐Pe=er Halvorsen, M.Sc.
![Page 11: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/11.jpg)
Data Tier We are going to create the Database / Data Layer/Tier, including: 1. Tables 2. Views 3. Stored Procedures 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)
11
Note! Install them in this order
![Page 12: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/12.jpg)
Data Tier
12
Tables
Views
Stored Procedures
SQL Server
Triggers
Data Tier
![Page 13: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/13.jpg)
13
Database Tables
![Page 14: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/14.jpg)
14
Execute the different Scripts inside SQL Server Management Studio
![Page 15: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/15.jpg)
15 You are finished with the Exercise
![Page 16: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/16.jpg)
Crea/ng the Logic Tier
Hans-‐Pe=er Halvorsen, M.Sc.
![Page 17: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/17.jpg)
Logic Tier
17
Database
Presenta/on Tier
Logic Tier
ASP.NET Web Forms
Data Tier
Presenta/on Tier Presenta/on 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 18: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/18.jpg)
18
Create an Empty (Blank) Solu4on in Visual Studio
![Page 19: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/19.jpg)
19
Add Project for Logic Tier (Data Access)
Select a “Class Library” Project
“LogicTier”
![Page 20: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/20.jpg)
20
Add a New Class to the Project (“StudentData.cs”)
“StudentData.cs”
![Page 21: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/21.jpg)
21
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 22: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/22.jpg)
22
You should test the SQL Query in the SQL Server Management Studio first
![Page 23: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/23.jpg)
23
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 24: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/24.jpg)
24
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 /mes!!
Then Build your Project (hopefully with no errors)
Right-‐click on the Project in the Solu/on Explorer and select Proper/es
![Page 25: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/25.jpg)
25 You are finished with the Exercise
![Page 26: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/26.jpg)
Crea/ng the Presenta/on Tier
Hans-‐Pe=er Halvorsen, M.Sc.
![Page 27: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/27.jpg)
27
Presenta/on Layer Desktop App: WinForms
We assume the App will be used only in the local LAN (or local on the same computer where the database is located) and that we have direct access to the Database)
Label
DataGridView
![Page 28: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/28.jpg)
28
Add a WinForm Project
![Page 29: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/29.jpg)
29
Add a New Class (“StudentWinForm.cs”)
![Page 30: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/30.jpg)
30
Add Code in Class
Add a Reference to the Assembly in the Logic Tier
![Page 31: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/31.jpg)
31
Code for Class “StudentWinForm.cs”
using System.Data; using Tuc.School.LogicTier; namespace Tuc.School.WinFormApp { class StudentWinForm { public DataSet GetStudent(string connectionString) { StudentData studentData = new StudentData(); return studentData.GetStudentDB(connectionString); } } }
Reference to our Logic Tier
Since we are using the DataSet Class
Our Database Method in our Logic Tier
![Page 32: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/32.jpg)
32
Create Form
Label
DataGridView
![Page 33: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/33.jpg)
33
Create Form Code
![Page 34: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/34.jpg)
34
using System.Configuration; using Tuc.School.WinFormApp; namespace WinFormApp { public partial class Form1 : Form { private string connectionString =
ConfigurationManager.ConnectionStrings["SCHOOLConnectionString"].ConnectionString; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { FillStudentGrid(); } private void FillStudentGrid() { DataSet ds = new DataSet(); StudentWinForm studentList = new StudentWinForm(); ds = studentList.GetStudent(connectionString); dataGridViewStudentInformation.DataSource = ds.Tables[0]; } } }
WinForm Code Note!
Connec/onString is stored in App.config
![Page 35: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/35.jpg)
35
Note! Add System.Configura/on Reference
![Page 36: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/36.jpg)
36
Create DB Connec/onString in App.config <?xml version="1.0" encoding="utf-‐8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <connectionStrings> <add name="SCHOOLConnectionString" connectionString="Data Source=macwin8;Initial Catalog=SCHOOL;Persist Security Info=True;User ID=sa;Password=xxxxxx" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration>
![Page 37: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/37.jpg)
37
Test it
It works!!!
![Page 38: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/38.jpg)
38 You are finished with the Exercise
![Page 39: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/39.jpg)
Recommended Li=erature
• Tutorial: Introduc/on to Database Systems h=p://home.hit.no/~hansha/?tutorial=database
• Tutorial: Structured Query Language (SQL) h=p://home.hit.no/~hansha/?tutorial=sql
• Tutorial: Using SQL Server in C# • Tutorial: Introduc/on to Visual Studio and C# h=p://home.hit.no/~hansha/?tutorial=csharp
39
![Page 40: Database Communication in C# using 3 Layer Architecture](https://reader034.vdocument.in/reader034/viewer/2022052419/5874ba1e1a28ab9e0d8bdeae/html5/thumbnails/40.jpg)
Hans-‐PeUer Halvorsen, M.Sc. Telemark University College Faculty of Technology Department of Electrical Engineering, Informa4on Technology and Cyberne4cs
E-‐mail: [email protected] Blog: hUp://home.hit.no/~hansha/
40