Chapter 15: Using LINQ to Access Data in C# Programs.
-
Upload
felicia-white -
Category
Documents
-
view
228 -
download
0
Transcript of Chapter 15: Using LINQ to Access Data in C# Programs.
![Page 1: Chapter 15: Using LINQ to Access Data in C# Programs.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e725503460f94b70d06/html5/thumbnails/1.jpg)
Chapter 15:Using LINQ to Access Data in
C# Programs
![Page 2: Chapter 15: Using LINQ to Access Data in C# Programs.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e725503460f94b70d06/html5/thumbnails/2.jpg)
2Microsoft Visual C# 2012, Fifth Edition
Understanding Relational Database Fundamentals
• Database– Holds a file or, more frequently, a group of files that an
organization needs to support its applications• In a database, the files often are called tables
because you can arrange their contents in rows and columns
• Primary key (or key)– A value that uniquely identifies a record
![Page 3: Chapter 15: Using LINQ to Access Data in C# Programs.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e725503460f94b70d06/html5/thumbnails/3.jpg)
3Microsoft Visual C# 2012, Fifth Edition
Understanding Relational Database Fundamentals (cont’d.)
![Page 4: Chapter 15: Using LINQ to Access Data in C# Programs.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e725503460f94b70d06/html5/thumbnails/4.jpg)
4Microsoft Visual C# 2012, Fifth Edition
Understanding Relational Database Fundamentals (cont’d.)
• Database management software – Also called a database management system (DBMS)– Allows you to:
• Create table descriptions• Identify keys• Add, delete, and update records within a table• Arrange records within a table so they are sorted by different
fields• Write questions that select specific records from a table for
viewing• Write questions that combine information from multiple tables• Create reports that allow users to easily interpret your data• Keep data secure by employing sophisticated security measures
![Page 5: Chapter 15: Using LINQ to Access Data in C# Programs.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e725503460f94b70d06/html5/thumbnails/5.jpg)
5Microsoft Visual C# 2012, Fifth Edition
Understanding Relational Database Fundamentals (cont’d.)
• Database management software (cont’d.)– Establishes and maintains relationships between the
columns in tables• A group of database tables from which you can make these
connections is a relational database
![Page 6: Chapter 15: Using LINQ to Access Data in C# Programs.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e725503460f94b70d06/html5/thumbnails/6.jpg)
6Microsoft Visual C# 2012, Fifth Edition
Creating Databases and Table Descriptions (cont’d.)
![Page 7: Chapter 15: Using LINQ to Access Data in C# Programs.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e725503460f94b70d06/html5/thumbnails/7.jpg)
7Microsoft Visual C# 2012, Fifth Edition
Identifying Primary Keys
• The primary key in a table is the column that makes each record different from all others– Each primary key must be unique
• Typical examples of primary keys include:– A student ID number in a table that contains college
student information– A part number in a table that contains inventory items– A Social Security number in a table that contains employee
information
![Page 8: Chapter 15: Using LINQ to Access Data in C# Programs.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e725503460f94b70d06/html5/thumbnails/8.jpg)
8Microsoft Visual C# 2012, Fifth Edition
Creating SQL Queries
• Query– Simply a request using syntax that the database software
can understand• Structured Query Language (or SQL)
– The most common language that database administrators use to access data in their tables
• The basic form of the SQL command that retrieves selected records from a table– SELECT-FROM-WHERE
• Example:SELECT custId, lastName FROM tblCustomer WHERE state = "WI"
![Page 9: Chapter 15: Using LINQ to Access Data in C# Programs.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e725503460f94b70d06/html5/thumbnails/9.jpg)
9Microsoft Visual C# 2012, Fifth Edition
Creating an Access Database
• Microsoft Office Access– A relational database that is part of some versions of the
Microsoft Office system
![Page 10: Chapter 15: Using LINQ to Access Data in C# Programs.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e725503460f94b70d06/html5/thumbnails/10.jpg)
10Microsoft Visual C# 2012, Fifth Edition
Creating an Access Database (cont’d.)
![Page 11: Chapter 15: Using LINQ to Access Data in C# Programs.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e725503460f94b70d06/html5/thumbnails/11.jpg)
11Microsoft Visual C# 2012, Fifth Edition
Creating an Access Database (cont’d.)
![Page 12: Chapter 15: Using LINQ to Access Data in C# Programs.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e725503460f94b70d06/html5/thumbnails/12.jpg)
12Microsoft Visual C# 2012, Fifth Edition
Understanding LINQ
• LINQ (Language INtegrated Query) Project– Provides queries that can be used in C# and Visual Basic
• In older versions of C#, you could access database data by passing an SQL string to a database object– OleDbCommand is a built-in type used to access
databases• LINQ provides a set of general-purpose standard
operators that allow queries to be constructed using syntax that is easy to understand
![Page 13: Chapter 15: Using LINQ to Access Data in C# Programs.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e725503460f94b70d06/html5/thumbnails/13.jpg)
13Microsoft Visual C# 2012, Fifth Edition
Understanding LINQ (cont’d.)
• The operators defined in LINQ can be used to query arrays, enumerable classes, XML, relational databases, and other sources
• Some keywords in the LINQ vocabulary:– select indicates what to select– from indicates the collection or sequence from which data
will be drawn– where indicates conditions for selecting records
![Page 14: Chapter 15: Using LINQ to Access Data in C# Programs.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e725503460f94b70d06/html5/thumbnails/14.jpg)
14Microsoft Visual C# 2012, Fifth Edition
Retrieving Data from an Access Database in C#
• Save time and reduce the chance for error by using the built-in tools of the Visual Studio IDE
• Tasks for adding a database table to a Windows Form project:– Drag a DataGridView object from the IDE’s Toolbox
onto a Form• Next, browse for the project data source and answer a few
questions– Make a few choices and browse for the database file to
add to the project
![Page 15: Chapter 15: Using LINQ to Access Data in C# Programs.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e725503460f94b70d06/html5/thumbnails/15.jpg)
15Microsoft Visual C# 2012, Fifth Edition
Retrieving Data from an Access Database in C# (cont’d.)
![Page 16: Chapter 15: Using LINQ to Access Data in C# Programs.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e725503460f94b70d06/html5/thumbnails/16.jpg)
16Microsoft Visual C# 2012, Fifth Edition
Retrieving Data from an Access Database in C# (cont’d.)
![Page 17: Chapter 15: Using LINQ to Access Data in C# Programs.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e725503460f94b70d06/html5/thumbnails/17.jpg)
17Microsoft Visual C# 2012, Fifth Edition
Retrieving Data from an Access Database in C# (cont’d.)
![Page 18: Chapter 15: Using LINQ to Access Data in C# Programs.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e725503460f94b70d06/html5/thumbnails/18.jpg)
18Microsoft Visual C# 2012, Fifth Edition
Retrieving Data from an Access Database in C# (cont’d.)
![Page 19: Chapter 15: Using LINQ to Access Data in C# Programs.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e725503460f94b70d06/html5/thumbnails/19.jpg)
19Microsoft Visual C# 2012, Fifth Edition
Using LINQ Queries with an Access Database Table
• You are not required to use the data grid to view database records
• You can use a LINQ query to select a collection of data from a table when specific criteria are met
![Page 20: Chapter 15: Using LINQ to Access Data in C# Programs.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e725503460f94b70d06/html5/thumbnails/20.jpg)
20Microsoft Visual C# 2012, Fifth Edition
Using LINQ Queries with an Access Database Table (cont’d.)
![Page 21: Chapter 15: Using LINQ to Access Data in C# Programs.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e725503460f94b70d06/html5/thumbnails/21.jpg)
21Microsoft Visual C# 2012, Fifth Edition
Using LINQ Queries with an Access Database Table (cont’d.)
![Page 22: Chapter 15: Using LINQ to Access Data in C# Programs.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e725503460f94b70d06/html5/thumbnails/22.jpg)
22Microsoft Visual C# 2012, Fifth Edition
Using LINQ Queries with an Access Database Table (cont’d.)
![Page 23: Chapter 15: Using LINQ to Access Data in C# Programs.](https://reader035.fdocuments.net/reader035/viewer/2022062304/56649e725503460f94b70d06/html5/thumbnails/23.jpg)
23Microsoft Visual C# 2012, Fifth Edition
Using LINQ Queries with an Access Database Table (cont’d.)