Further exploring database operations from C#/.NET Jim Warren, COMPSCI 280 S2 2015 Enterprise Software Development.

Slides:



Advertisements
Similar presentations
Connecting an MVC application to a database Jim Warren, COMPSCI 280 S Enterprise Software Development.
Advertisements

PHP (2) – Functions, Arrays, Databases, and sessions.
Introduction to Structured Query Language (SQL)
SQL Data Definition II Stanislava Armstrong 1SQL Data Definition II.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Concepts of Database Management Sixth Edition
Database Design Concepts INFO1408 Term 2 week 1 Data validation and Referential integrity.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Further exploring database operations in MVC Jim Warren, COMPSCI 280 S Enterprise Software Development.
Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
Access Lecture 1 Database Overview and Creating Tables Create an Employee Table.
ORM Technologies and Entity Framework (EF)
MIS2502: Data Analytics MySQL and SQL Workbench David Schuff
CORE 2: Information systems and Databases STORAGE & RETRIEVAL 2 : SEARCHING, SELECTING & SORTING.
® IBM Software Group © 2006 IBM Corporation The Eclipse Data Perspective and Database Explorer This section describes how to use the Eclipse Data Perspective,
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Database Systems Lecture 5 Natasha Alechina
Page 1 ISMT E-120 Desktop Applications for Managers Introduction to Microsoft Access.
ACCESS CHAPTER 1. OBJECTIVES Tables Queries Forms Reports Primary and Foreign Keys Relationship.
Databases From A to Boyce Codd. What is a database? It depends on your point of view. For Manovich, a database is a means of structuring information in.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
ASP.NET Programming with C# and SQL Server First Edition
Connecting a.NET application to a database Jim Warren, COMPSCI 280 S Enterprise Software Development.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
DAY 15: ACCESS CHAPTER 2 Larry Reaves October 7,
Relational Database Concepts. Let’s start with a simple example of a database application Assume that you want to keep track of your clients’ names, addresses,
Chapter 4 The Relational Model 3: Advanced Topics Concepts of Database Management Seventh Edition.
Introduction to SQL Steve Perry
Chapter 15: Using LINQ to Access Data in C# Programs.
ADO.NET A2 Teacher Up skilling LECTURE 3. What’s to come today? ADO.NET What is ADO.NET? ADO.NET Objects SqlConnection SqlCommand SqlDataReader DataSet.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Programming using C# Joins SQL Injection Stored Procedures
RDBMSSection Relational DBMS DATABASE DEVELOPMENT And Franchise Colleges By MANSHA NAWAZ.
DAY 12: DATABASE CONCEPT Tazin Afrin September 26,
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
M1G Introduction to Database Development 5. Doing more with queries.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
INFO1408 Database Design Concepts Week 15: Introduction to Database Management Systems.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
1 CSE 2337 Introduction to Data Management Access Book – Ch 1.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Information Building and Retrieval Using MySQL Track 3 : Basic Course in Database.
G045 Lecture 08 DFD Level 1 Diagrams (Data Flow Diagrams Level 1)
Chapter 4 Introduction to Classes, Objects, Methods and strings
1 CS 430 Database Theory Winter 2005 Lecture 4: Relational Model.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
Session 1 Module 1: Introduction to Data Integrity
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Access Module Implementing a Database with Microsoft Access A Great Module on Your CD.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
MYSQL AND MYSQL WORKBENCH MIS2502 Data Analytics.
Programming for the Web MySQL Command Line Using PHP with MySQL Dónal Mulligan BSc MA
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
Chapter 12 Introducing Databases. Objectives What a database is and which databases are typically used with ASP.NET pages What SQL is, how it looks, and.
Introduction to Entity Framework
Database application MySQL Database and PhpMyAdmin
mysql and mysql workbench
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
MIS2502: Data Analytics MySQL and SQL Workbench
[Robert W. Sebesta, “Programming the World Wide Web
Presentation transcript:

Further exploring database operations from C#/.NET Jim Warren, COMPSCI 280 S Enterprise Software Development

Today’s learning objectives  To be able to use the LINQ (language-integrate query) approach for database queries in C#/.NET  To be able to update the database from C#/.NET  To conceptualise an application in terms of CRUD (create, read, update, delete) operations  To consider the state of an object instance in relation to the database context COMPSCI 2802

Last lecture…  We learned to connect to a database, set up a model of its tables and create queries like:  This is OK, but it has some disadvantages  We don’t get support from the IDE for the query itself  It’s an arbitrary string – no type checking, no intellisense  It’s a bit of a pain to always need to figure out the type of the returned instances  It’s also a potential security hole… COMPSCI 2803 var x2 = db.Database.SqlQuery ("select * from employee");

SQL injection COMPSCI 2804

The LINQ approach  Makes the query actually a statement in the C# language  SQL’s select-from-where is turned on its head a bit  Makes the intellisense work better to use from-where-select order  In this case the return value is a collection of objects of a class we’ve already defined in our Model  Because we had said: public DbSet Employees { get; set; } and had defined the Employee class with property names exactly aligned to the columns of the DBMS table COMPSCI 2805 using (EmployeesContext db = new EmployeesContext()) { var emps = from e in db.Employees where e.DateOfBirth.Year < 1975 select e; string surname1=emps.First().surname } We get access to all C#’s operators, too!

LINQ Join  We could, of course, have sent join queries to MySQL as a string, but LINQ brings Join into C#  Notice the ‘select’  Even if we included in the model definitions of the ‘categories’ and ‘products’ tables, we’d need a new class to hold the combination of a product name and a category name  The ‘new’ statement is constructing an anonymous type with properties ProductName and Category  innerJoinQuery will be implicitly typed to a generic collection of instances with the generic’s type parameter being this new 2-property anonymous type  We can still do a foreach statement on innerJoinQuery to loop through the results COMPSCI 2806 var innerJoinQuery = from category in db.categories join prod in db.products on category.ID equals prod.CategoryID select new { ProductName = prod.Name, Category = category.Name }; Notice we said ‘equals’ as a keyword (not the = sign); this gives system most flexibility to exploit indices or other optimise the join

More LINQ – group by  Get a count of how many employees with each surname  Use ‘group’… ‘by’ keyword pair  Note use of ‘into’ and naming a second index variable (‘g’ in this case)  Note the special property Key is the thing we grouped by COMPSCI 2807 var cnts = from emp in db.Employees group emp by emp.Surname into g select new {s=g.Key, cnt=g.Count()}; foreach (var c in cnts) Console.WriteLine("Key: " + c.s + " Count: " + c.cnt);

Or method based GroupBy  Use GroupBy method allowing a lambda to specify just what we want the grouping Key to be  And you can apply one method onto the result of another COMPSCI 2808 foreach (var c in db.Employees.GroupBy(a=>a.Surname.Substring(0,1))) Console.WriteLine("Key2: " + c.Key+ " Count2: " + c.Count()); foreach (var c in db.Employees.GroupBy(a => Math.Floor(a.DateOfBirth.Year/10f))) Console.WriteLine("Key2: " + c.Key + "0's Count2: " + c.Count()); foreach (var c in db.Employees.Where(a=>a.Surname.CompareTo("F")>=0).GroupBy(a=>a.Su rname.Substring(0,1))) Console.WriteLine("Key2: " + c.Key+ " Count2: " + c.Count()); Count of employees by first letter of surname Count of employees by decade of birth

What about the rest of the CRUD?!  CRUD: Create, Read, Update, Delete  A CRUD matrix can be a useful specification for the scope of programming tasks  E.g. to describe the ‘life cycle’ of each entity in a system; e.g. a hotel reservation  On some screen (e.g. ‘booking’) it is created (SQL INSERT, not CREATE like making a new table)  There may be a screen to UPDATE it (e.g. ‘change booking’) which probably also reads the current value (‘R’ of CRUD, SQL SELECT)  And there will be one or more ways to DELETE it (e.g. from a cancellation action on the change booking screen or a dedicated cancellation screen, and also once the person has checked in) – then again, you might not actually delete in a SQL sense, but UPDATE it to cancelled or utilised status COMPSCI 2809

CRUD matrix (well, for one entity) Program (or ‘screen’)Entity=reservation Make reservationC Check reservationR Review/change reservationRU Review/cancel reservationRD* Client arrives (takes up reserved room)D** COMPSCI  For a more involved system you may have many entities represented as further columns  Each cell marked with a subset of the letters in ‘CRUD’ * Or update status to cancelled ** Or update status to taken

The UPDATE  Native SQL version  Given that I have a reference to an object emp of class Employee with updated values: COMPSCI using (EmployeesContext db = new EmployeesContext()) { string sql=String.Format( "UPDATE employee SET Surname='{0}',GivenNames='{1}',"+ "DateOfBirth='{2:yyyy-MM-dd}' WHERE id={3}", emp.Surname,emp.GivenNames,emp.DateOfBirth,emp.id); db.Database.ExecuteSqlCommand(sql);... } Here we’re just building the text of a SQL command with the help of String.Format to plug in the parameters from our C# code at the curly braces (note the 3 rd parameter - #2 counting from 0! – is formatted so MySQL recognises the date literal; also note carefully the double quotes interpreted by C# and the signle quotes that are for MySQL Then we just.ExecuteSqlCommand (as compared to doing. SqlQuery) on the database context to tell MySQL to have at it Re-establish the database connection

Native INSERT  Similar to the UPDATE, once we have a database context established we can do INSERT (or DELETE) SQL COMPSCI string sql_ins = String.Format("INSERT INTO employee (GivenNames,SurName,DateOfBirth) "+ "VALUES ('{0}','{1}','{2:yyyy-MM-dd}')","Bob", "Barker",DateTime.Now); db.Database.ExecuteSqlCommand(sql_ins); string sql_del = "delete from employee where GivenNames='Bob'"; db.Database.ExecuteSqlCommand(sql_del);

The UPDATE v2  Entity Framework version  If you’ve gotten an object from the database, the system tracks that that object is “attached” to the database context  If you want to update a record  Retrieve it  Change properties of the object as you wish  Save the changes COMPSCI using (EmployeesContext db = new EmployeesContext()){ var updEmp = from emp in db.Employees where emp.Surname == "Barker" select emp; foreach (Employee e3 in updEmp) e3.Surname = "Good"; db.SaveChanges();... } Every employee with surname ‘Barker’ gets updated to surname ‘Good’

INSERT by adding  If you simply create a ‘new’ object of the class of a record, it isn’t attached to the database context by default  However, you use the appropriate DbSet’s Add method to attach it  Use the database context’s SaveChanges method to make the insertion stick COMPSCI using (EmployeesContext db = new EmployeesContext()) {... var e1 = new Employee{GivenNames="Tommy", Surname="Tank"}; db.Employees.Add(e1); db.SaveChanges(); Adds a new employee with name “Tommy Tank”, letting the ‘id’ be supplied automatically and leaving DateOfBirth unspecified (probably will be set to 1 Jan 1 AD !) Note curly-brace “object initializer” syntax. Invokes constructor and then assigns the specified values to the named properties of the new object

Delete  To delete  Get a handle on the relevant object(s) (e.g. with a query, like we did for update)  Invoke the Remove method of the DbSet with the object to delete as a parameter  Note: if deleting (or in some cases updating) where there are foreign key constraints you may need to think carefully about the ‘cascade’ of effects with respect to referential integrity (see us/library/aa902684(v=sql.80).aspx) COMPSCI using (EmployeesContext db = new EmployeesContext()) {... var deletedEmp = from emp in db.Employees where emp.Surname == "Tank" select emp; foreach (Employee emp in deletedEmp) db.Employees.Remove(emp); db.SaveChanges();

Where we’re up to  We’ve learned some basics of.NET and C#  We’ve seen that we can interact with our DBMS by sending it native SQL or using C#/.NET language-embedded syntax (LINQ and Entity Framework)  Now…  Work the second labsheet (if you haven’t already)  Get seriously into Assignment 2  See you other side of the break!  FYI, I’m on leave first week of the break, but will be at work and answering questions second week of the break COMPSCI 28016