Download Database Communication using Web Services

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Database wikipedia , lookup

Microsoft Access wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Relational model wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Database model wikipedia , lookup

Clusterpoint wikipedia , lookup

Transcript
Database Communica/on in Visual Studio/C# using Web Services Hans-­‐Pe=er Halvorsen, M.Sc. Background •  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 SoKware Architecture 3-­‐Tier: A way to structure your code into logical parts. Different devices or soKware modules can share the same code. Web Services Client-­‐
Server 3-­‐Tier Architecture Good SoKware! Web Services: A standard way to get data over a network/Internet using standard Web protocols (HTTP, etc.) 2-­‐Tier APIs n-­‐Tier API: Applica/on Programming Interface. Different devices or soKware modules can share the same code. Code once, use it many /mes 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. 4 Note! The different layers can be on the same computer (Logic Layers) or on different Computers in a network (Physical Layers) 3-­‐/er/layer Architecture Presenta/on Tier PL Business Logic Tier BL Logic Tier Data Access Tier Data Source DAL Data Tier -­‐ DL 5 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 Pla\orms and Languages can be used •  etc. 6 h=p://en.wikipedia.org/wiki/Mul//er_architecture 7 3-­‐/er/layer Architecture PresentaAon 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 ApplicaAon Aer (business logic, logic Aer, data access Aer, or middle Aer) •  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 Aer •  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. h=p://en.wikipedia.org/wiki/Mul//er_architecture 8 3-­‐/er Architecture Presenta/on Tier Presenta/on Tier Presenta/on Tier Business Logic Tier Different Devices can share the same Business and Data Access Code The different Tiers can be physical or logical Logic Tier Data Access Tier Stored Procedures Database Data Tier 3-­‐/er + WebService Architecture -­‐ Example Team Founda/on Server Installed on one or more Windows Servers in your LAN or in the Cloud Web Web Server Services Business/
Data Logic Tier Stored Procedures Data Source Data Tier Team Founda/on Server TFS Cient Presenta/on Tier 10 3-­‐/er Architecture Scenarios Client Client Client Internet Presenta/on Layer Firewall Presenta/on Layer Presenta/on Layer Client Client Web Service Presenta/on Layer Presenta/on Layer Presenta/on Layer Web Server Server Local Network (LAN) Stored Database Procedures Server Business Logic Data Access Logic WinForms Client Client Android, iOS, Windows 8/Windows Phone, etc. Client Client Mobile App Presenta/on Tier Desktop App Seperate Presenta/on Tier Internet PresentaAon Tiers for each Device App Presenta/on Tier Web App Firewall Presenta/on Tier ASP.NET Web Forms Devices can share the same Business/Logic Tier and APIs Clients API API Database Server API Web Server Web Service Local Network API Business Tier Logic Tier Data Access Tier e.g., ADO, ADO.NET Database Stored Procedures Views Tables Data Tier 3-­‐/er Architecture Scenarios Note! The different Tiers can be on the same Computer (Logic Layers) or on different Computers in a network (Physical Layers) Visual Studio Projects Solu/on with all Projects (Logic Tier, Web Service, Desktop App, Web App, Mobile App) Solu/on 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 in this order 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) 14 Data Tier Triggers Stored Procedures Views Tables Data Tier 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 Presenta/on Tier Windows Store App WinForms Presenta/on Tier Presenta/on Tier Logic Tier Data Tier Purpose: •  All the Apps should/could share the same Logic Tier •  To make your Apps easier to maintain and extend •  etc. Database 19 Create an Empty (Blank) SoluAon 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 Solu/on Explorer and select Proper/es 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 /mes!! 26 You are finished with the Exercise 27 Presenta/on 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 Connec/onString is located in Web.config Web Service Method 30 Database Connec/onString 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: h=p://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(); } Call the Web Service method private void FillStudentGrid() { 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 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 43 Hans-­‐PeUer Halvorsen, M.Sc. Telemark University College Faculty of Technology Department of Electrical Engineering, InformaAon Technology and CyberneAcs E-­‐mail: [email protected] Blog: hUp://home.hit.no/~hansha/ 44