LINQ to DATABASE-2.  Creating the BooksDataContext  The code combines data from the three tables in the Books database and displays the relationships.

Slides:



Advertisements
Similar presentations
Advanced SQL Topics Edward Wu.
Advertisements

© Abdou Illia MIS Spring 2014
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
4d. Structured Query Language – JOIN Operation Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis with Spreadsheets.
 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.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
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
A tour of new features introducing LINQ. Agenda of LINQ Presentation We have features for every step of the way LINQ Fundamentals Anonymous Functions/Lambda.
Chapter 18 Databases and LINQ Visual C# 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
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.
Introduction to Databases Chapter 7: Data Access and Manipulation.
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,
 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.
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:
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.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
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.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
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.
CHAPTER 1 – INTRODUCTION TO ACCESS Aliya Farheen March 5, 2014.
 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.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
 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.
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.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
CSC314 DAY 9 Intermediate SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall USING AND DEFINING VIEWS  Views provide users controlled.
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
 2012 Pearson Education, Inc. All rights reserved.
Visual Basic 2010 How to Program
LINQ to DATABASE-2.
JDBC.
The University of Texas – Pan American
David M. Kroenke and David J
Structured Query Language (SQL) William Klingelsmith
Introduction to LINQ Chapter 11 10/28/2015 Lect 4 CT1411.
Chapter 22 - SQL, MySQL, DBI and ADO
LINQ to DATABASE-2.
Introduction to LINQ Chapter 11.
Chapter 8 Advanced SQL.
Queries and SQL in Access
Manipulating Data Lesson 3.
Presentation transcript:

LINQ to DATABASE-2

 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 , line 7) allows the program to interact with the database. © by Pearson Education, Inc. All Rights Reserved.

 Combining Author Names with the ISBNs of the Books They’ve Written  The first query (Fig , 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 Combining Author Names with the Titles of the Books They’ve Written  The second query (Fig , 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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 , lines 45–51) returns hierarchical results.  Each element in the results contains the name of an Author and a list of Title s 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.