LINQ to DATABASE-2.

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. Chapter 22 – Database: SQL, MySQL, DBI and ADO.NET Outline 22.1 Introduction 22.2 Relational Database Model.
Advertisements

CSCI 3328 Object Oriented Programming in C# Chapter 12: Databases and LINQ 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Exercise SELECT authorID, lastName FROM authors AuthorID FirstName
Chapter 18 Databases and LINQ Visual C# 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Databases and LINQ Visual Basic 2010 How to Program 1.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
 2008 Pearson Education, Inc. All rights reserved Database: SQL, MySQL, ADO.NET 2.0 and Java DB.
CHAPTER 7 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
 2003 Prentice Hall, Inc. All rights reserved. 1 Java Database Connectivity with JDBC TM.
CHAPTER 8 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 30 – Bookstore Application: Client Tier Examining.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Database: SQL and MySQL
SQL 101 for Web Developers 14 November What is a database and why have one? Tables, relationships, normalization SQL – What SQL is and isn’t – CRUD:
1 Databases November 15, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel, and Goldberg. Published.
 2004 Prentice Hall, Inc. All rights reserved. 1 Segment – 6 Web Server & database.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Introduction to LINQ Chapter 11. Introduction Large amounts of data are often stored in a database—an organized collection of data. A database management.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
Subqueries Steve Perry 1.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved. 2 Revised by Dr. T. Tran for CSI3140.
 2008 Pearson Education, Inc. All rights reserved Database: SQL, MySQL, ADO.NET 2.0 and Java DB.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 22 - SQL, MySQL, DBI and ADO Outline 22.1 Introduction 22.2 Relational Database Model 22.3 Relational.
 2009 Pearson Education, Inc. All rights reserved Databases and LINQ to SQL.
CSCI 3328 Object Oriented Programming in C# Chapter 12: Databases and LINQ – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
LINQ to DATABASE-2.  Creating the BooksDataContext  The code combines data from the three tables in the Books database and displays the relationships.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
Joining Tables Steve Perry
TECHNOLOGY IN ACTION. Chapter 11 Behind the Scenes: Databases and Information Systems.
© 2016, Mike Murach & Associates, Inc.
ORDER BY Clause The result of a query can be sorted in ascending or descending order using the optional ORDER BY clause. The simplest form of.
Database, SQL and ADO.NET
Visual Basic 2010 How to Program
Database Programming in Java
Databases Chapter 16.
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
© 2016, Mike Murach & Associates, Inc.
 2012 Pearson Education, Inc. All rights reserved.
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
Visual Basic 2010 How to Program
JDBC.
Databases Intro (from Deitel)
The University of Texas – Pan American
INFO/CSE 100, Spring 2005 Fluency in Information Technology
David M. Kroenke and David J
SQL 101.
Structured Query Language (SQL) William Klingelsmith
Chapter 22 - SQL, MySQL, DBI and ADO
Normalization By Jason Park Fall 2005 CS157A.
LINQ to DATABASE-2.
Introduction to LINQ Chapter 11.
INFO/CSE 100, Spring 2006 Fluency in Information Technology
Data Management Innovations 2017 High level overview of DB
M1G Introduction to Database Development
Introduction To Structured Query Language (SQL)
Advanced Database Concepts: Reports & Views
Introduction to Access
Projecting output in MySql
Databases and the MVC Model
Queries and SQL in Access
Normalization By Jason Park Fall 2005 CS157A.
Manipulating Data Lesson 3.
Microsoft Access Date.
Presentation transcript:

LINQ to DATABASE-2

12.7 Retrieving Data from Multiple Tables with LINQ Creating the BooksDataContext The code combines data from the three tables in the Books database and displays the relationships between the book titles and authors in three different ways. It uses LINQ to SQL classes that have been created using the same steps as the previous example. As in previous examples, the BooksDataContext object (Fig. 12.22, line 7) allows the program to interact with the database. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

© 1992-2011 by Pearson Education, Inc. All Rights Reserved.

12.7 Retrieving Data from Multiple Tables with LINQ Combining Author Names with the ISBNs of the Books They’ve Written The first query (Fig. 12.23, lines 11–14) joins data from two tables and returns a list of author names and the ISBNs representing the books they’ve written, sorted by LastName then FirstName. The query takes advantage of the properties that LINQ to SQL creates based on foreign-key relationships between the database’s tables. These properties enable you to easily combine data from related rows in multiple tables. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

© 1992-2011 by Pearson Education, Inc. All Rights Reserved.

12.7 Retrieving Data from Multiple Tables with LINQ The first From clause (line 11) gets one author from the Authors table. The second From clause (line 12) uses the generated AuthorISBNs property of the Author class to get only the rows in the AuthorISBN table that link to the current author—that is, the ones that have the same AuthorID as the current author. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

12.7 Retrieving Data from Multiple Tables with LINQ The combined result of the two From clauses is a collection of all the authors and the ISBNs of the books they’ve authored. The two From clauses introduce two range variables into the scope of this query—other clauses can access both range variables to combine data from multiple tables. Line 14 combines the FirstName and LastName of an author from the Authors table with a corresponding ISBN from the AuthorISBNs table. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

12.7 Retrieving Data from Multiple Tables with LINQ Combining Author Names with the Titles of the Books They’ve Written The second query (Fig. 12.24, lines 27–31) gives similar output, but uses the foreign-key relationships to go one step further and get the actual title of each book that an author wrote. The first From clause (line 27) gets one title from the Titles table. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

12.7 Retrieving Data from Multiple Tables with LINQ The second From clause (line 28) uses the generated AuthorISBNs property of the Title class to get only the rows in the AuthorISBN table that link to the current title—that is, the ones that have the same ISBN as the current title. Each of those book objects contains an Author property that represents the foreign-key relationship between the AuthorISBNs table and the Authors table. This Author property gives us access to the names of the authors for the current book. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

© 1992-2011 by Pearson Education, Inc. All Rights Reserved.

12.7 Retrieving Data from Multiple Tables with LINQ Line 29 introduces the Let query operator, which allows you to declare a new variable in a LINQ query—usually to create a shorter name for an expression. The variable can be accessed in later statements just like a range variable. The author variable created in the Let clause refers to book.Author. The Select clause (line 31) uses the author and title variables introduced earlier in the query to get the FirstName and LastName of each author from the Authors table and the Title of each book from the Titles table. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.