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
Queries and SQL in Access Please use speaker notes for additional information!
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.
College Board: 101 Books College Freshmen Should Read.
Databases and LINQ Visual Basic 2010 How to Program 1.
 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.
Chapter 9 Joining Data from Multiple Tables
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:
 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.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Programming in R SQL in R. Running SQL in R In this session I will show you how to: Run basic SQL commands within R.
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.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
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.
Chapter 11.  Large amounts of data are often stored in a database—an organized collection of data.  A database management system (DBMS) provides mechanisms.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Joining Tables Steve Perry
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.
Visual Basic 2010 How to Program
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Databases Chapter 16.
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
 2012 Pearson Education, Inc. All rights reserved.
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
Visual Basic 2010 How to Program
LINQ to DATABASE-2.
JDBC.
The University of Texas – Pan American
INFO/CSE 100, Spring 2005 Fluency in Information Technology
David M. Kroenke and David J
Structured Query Language (SQL) William Klingelsmith
Introduction to LINQ Chapter 11 10/28/2015 Lect 4 CT1411.
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections
Chapter 22 - SQL, MySQL, DBI and ADO
JOINS (Joinining multiple tables)
Introduction to LINQ Chapter 11.
Creating and Maintaining
INFO/CSE 100, Spring 2006 Fluency in Information Technology
SQL Subquery.
Combining Data Sets in the DATA step.
Introduction To Structured Query Language (SQL)
Contents Preface I Introduction Lesson Objectives I-2
Advanced Database Concepts: Reports & Views
CIS16 Application Development and Programming using Visual Basic.net
Introduction to Access
Chapter 8 Advanced SQL.
Queries and SQL in Access
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
Manipulating Data Lesson 3.
JOINS (Joinining multiple tables)
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. Equevalent query Dim authorsAndISBNs = From author In database.Authors From book In database.AuthorISBNs Order By author.LastName, author.FirstName Where author.AuthorID = book.AuthorID Select author.FirstName, author.LastName, book.ISBN © 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.

12.7 Retrieving Data from Multiple Tables with LINQ Organizing Book Titles by Author Most queries return results with data arranged in a relational- style table of rows and columns. The last query (Fig. 12.25, lines 45–51) returns hierarchical results. Each element in the results contains the name of an Author and a list of Titles that the author wrote. The LINQ query does this by using a nested query in the Select clause. The outer query iterates over the authors in the database. The inner query takes a specific author and retrieves all titles that the author worked on. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

12.7 Retrieving Data from Multiple Tables with LINQ The Select clause (lines 47–51) creates an anonymous type with two properties: The property Name (line 47) combines each author’s name, separating the first and last names by a space. The property Titles (line 48) receives the result of the nested query, which returns the Title of each book written by the current author. The nested For Each…Next statements (lines 57–67) use the properties of the anonymous type created by the query to output the hierarchical results. The outer loop displays the author’s name and the inner loop displays the titles of all the books written by that author. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

© 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 Notice the duplicate Title identifier in the expression book.Title.Title used in the inner Order By and Select clauses (lines 50–51). This is due to the database having a Title column in the Titles table, and is another example of following foreign- key relationships. The range variable book iterates over the rows of the AuthorISBN for the current author’s books. Each book’s Title property contains the corresponding row from the Titles table for that book. The second Title in the expression returns the Title column (the title of the book) from that row of the Titles. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.