2009 Pearson Education, Inc. All rights reserved Databases and LINQ to SQL
2009 Pearson Education, Inc. All rights reserved. 2 It is a capital mistake to theorize before one has data. – Arthur Conan Doyle Now go, write it before them in a table, and note it in a book, that it may be for the time to come for ever and ever. – Isaiah 30:8
2009 Pearson Education, Inc. All rights reserved. 3 Get your facts first, and then you can distort them as much as you please. – Mark Twain I like two kinds of men: domestic and foreign. – Mae West
2009 Pearson Education, Inc. All rights reserved. 4 OBJECTIVES In this chapter you will learn: The relational database model. To write basic database queries in SQL. To use LINQ to SQL to retrieve and manipulate data from a database. To add data sources to projects. To use the Object Relational Designer to create LINQ to SQL classes. To use the IDE’s drag-and-drop capabilities to display database tables in applications. To use data binding to move data seamlessly between GUI controls and databases. To create Master/Detail views.
2009 Pearson Education, Inc. All rights reserved Introduction 21.2 Relational Databases 21.3 Relational Database Overview: Books Database 21.4 SQL 21.5 LINQ to SQL 21.6 LINQ to SQL: Extracting Information from a Database 21.7 More Complex LINQ Queries and Data Binding 21.8 Retrieving Data from Multiple Tables with LINQ 21.9 Creating a Master/Detail View Application Programming with LINQ to SQL: Address-Book Case Study
2009 Pearson Education, Inc. All rights reserved Introduction A database is an organized collection of data. A database management system (DBMS) organizes data for many users. Relational databases organize data as tables with rows and columns. Structured Query Language (SQL) is the international standard language used with relational databases.
2009 Pearson Education, Inc. All rights reserved. 7 Figure 21.1 illustrates a sample Employees table. The ID column is the table ’ s primary key — a column used to uniquely identify a row. A primary key composed of two or more columns is a composite key. Fig | Employees table sample data Relational Databases
2009 Pearson Education, Inc. All rights reserved. 8 Each column represents a different data attribute. Most users of a database use only subsets of the rows and columns. Programs use SQL to define queries that select subsets of the data from a table (Fig. 21.2). Fig | Distinct Department and Location data from the Employees table Relational Databases (Cont.)
2009 Pearson Education, Inc. All rights reserved. 9 Fig | Authors table of the Books database. A database ’ s tables, their fields and the relationships between them are collectively known as a database schema. The Authors table has three fields, represented by columns (Fig. 21.3) Relational Database Overview: Books Database
2009 Pearson Education, Inc. All rights reserved. 10 Fig | Data from the Authors table of the Books database. Figure 21.4 contains the data from the Authors table Relational Database Overview: Books Database (Cont.)
2009 Pearson Education, Inc. All rights reserved Relational Database Overview: Books Database (Cont.) Fig | Titles table of the Books database. The Titles table (described in Fig. 21.5) consists of four columns.
2009 Pearson Education, Inc. All rights reserved Relational Database Overview: Books Database (Cont.) Fig | Data from the Titles table of the Books database. Figure 21.6 contains the data from the Titles table.
2009 Pearson Education, Inc. All rights reserved. 13 Fig | AuthorISBN table of the Books database. The AuthorISBN table (described in Fig. 21.7) matches the AuthorID and ISBN columns. These foreign keys form a composite primary key Relational Database Overview: Books Database (Cont.)
2009 Pearson Education, Inc. All rights reserved. 14 Fig | Data from the AuthorISBN table of Books. Figure 21.8 contains the data from the AuthorISBN table of the Books database Relational Database Overview: Books Database (Cont.)
2009 Pearson Education, Inc. All rights reserved. 15 Rule of Referential Integrity: every foreign-key value must be another table’s primary-key value. Foreign keys also allow related data in multiple tables to be joined. A foreign key can appear many times in a table but only once (as the primary key) in its original table Relational Database Overview: Books Database (Cont.)
2009 Pearson Education, Inc. All rights reserved. 16 Common Programming Error 21.2 Providing the same value for the primary key in multiple rows breaks the Rule of Entity Integrity and causes the DBMS to report an error. Common Programming Error 21.3 Providing a foreign-key value that does not appear as a primary-key value in another table breaks the Rule of Referential Integrity and causes the DBMS to report an error. Common Programming Error 21.2 Not providing a value for every column in a primary key breaks the Rule of Entity Integrity and causes the DBMS to report an error Relational Database Overview: Books Database (Cont.)
2009 Pearson Education, Inc. All rights reserved. 17 Figure 21.9 is an entity-relationship (ER) diagram for the Books database. Note that primary keys are italic. On the Authors end of the line, there is a 1, and on the AuthorISBN end, an infinity symbol (∞), indicating a one-to-many relationship. Fig | Entity-relationship diagram for the Books database Relational Database Overview: Books Database (Cont.)
2009 Pearson Education, Inc. All rights reserved. 18 Fig | Common SQL keywords. Figure lists some common SQL keywords SQL
2009 Pearson Education, Inc. All rights reserved Basic SELECT Query A SQL query “selects” rows and columns from one or more tables in a database. SELECT * FROM tableName The asterisk ( * ) indicates that all the columns from the tableName table should be retrieved. To retrieve all the data in the Authors table: SELECT * FROM Authors 21.4 SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 20 Fig | A uthorID and LastName data from the Authors table. To retrieve only specific columns, use a list of the column names: SELECT AuthorID, LastName FROM Authors This query returns the data listed in Fig SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved WHERE Clause Users can query a database for rows that satisfy certain selection criteria. The basic form of a query with selection criteria is SELECT columnName1, columnName2, FROM tableName WHERE criteria 21.4 SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 22 Fig | Books with copyright dates after 2007 from table Titles. To select books for which the Copyright is more recent than 2007: SELECT BookTitle, EditionNumber, Copyright. FROM Titles WHERE Copyright > '2007' Figure shows the result of the preceding query SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 23 Operator LIKE is used for pattern matching. A pattern with a percent character ( % ) searches for zero or more characters at the percent character’s position. The following query locates authors whose last names start with the letter D : SELECT AuthorID, FirstName, LastName FROM Authors WHERE LastName LIKE 'D%' 21.4 SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 24 Fig | Authors from the Authors table whose last names start with D. The preceding query selects the two rows shown in Fig SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 25 An underscore ( _ ) indicates a single wildcard character at that position. The following query locates authors whose last names have the letter y as the second letter. SELECT AuthorID, FirstName, LastName FROM Authors WHERE LastName LIKE '_y%' 21.4 SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 26 Fig | The only author from the Authors table whose last name contains y as the second letter. The preceding query produces the row shown in Fig SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved ORDER BY Clause Rows in the result can be sorted into ascending or descending order by using the optional ORDER BY clause. SELECT columnName1, columnName2, FROM tableName ORDER BY column ASC SELECT columnName1, columnName2, FROM tableName ORDER BY column DESC ASC specifies ascending order, DESC specifies descending order column specifies the column on which the sort is based SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 28 Fig | Authors from table Authors in ascending order by LastName. To obtain the list of authors in ascending order by last name (Fig ), use the query SELECT AuthorID, FirstName, LastName FROM Authors ORDER BY LastName ASC 21.4 SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 29 Fig | Authors from table Authors in descending order by LastName. To obtain the same list of authors in descending order by last name (Fig ), use SELECT AuthorID, FirstName, LastName FROM Authors ORDER BY LastName DESC 21.4 SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 30 Multiple columns can be used for sorting with an ORDER BY clause: ORDER BY column1 sortingOrder, column2 sortingOrder, … Note that the sortingOrder does not have to be identical for each column SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 31 SELECT BookTitle, EditionNumber,Copyright FROM Titles ORDER BY Copyright DESC, BookTitle ASC This query returns books sorted first in descending order by copyright date, then in ascending order by title (Fig ) SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 32 Fig | Data from Titles in descending order by Copyright and ascending order by BookTitle SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 33 The WHERE and ORDER BY clauses can be combined. ORDER BY must be the last clause in the query. SELECT ISBN, BookTitle, EditionNumber, Copyright FROM Titles WHERE BookTitle LIKE '%How to Program' ORDER BY BookTitle ASC 21.4 SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 34 The query results are shown in Fig Fig | Books from table Titles whose BookTitle s end with How to Program in ascending order by BookTitle SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved Retrieving Data from Multiple Tables: INNER JOIN Database designers typically normalize databases—i.e., split related data into separate tables to ensure that a database does not store redundant data. For example, we use a table to store “links” between authors and titles. If we did not separate this information into individual tables, we would need to include author information with each entry in the Titles table SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 36 Often, it is desirable to merge data from multiple tables into a single result. An INNER JOIN merges rows from two tables by testing for matching values in a column that is common to the tables: SELECT columnName1, columnName2, … FROM table1 INNER JOIN table2 ON table1. columnName = table2. columnName 21.4 SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 37 The ON clause specifies the columns from each table that are compared to determine which rows are merged. The following query produces a list of authors accompanied by the ISBNs for books written by each author: SELECT FirstName, LastName, ISBN FROM Authors INNER JOIN AuthorISBN ON Authors.AuthorID = AuthorISBN.AuthorID ORDER BY LastName, FirstName Common Programming Error 21.4 In a SQL query, failure to qualify names for columns that have the same name in two or more tables is an error SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 38 Figure depicts the results of the preceding query, ordered by LastName and FirstName. Fig | Authors and ISBNs for their books in ascending order by LastName and FirstName. (Part 1 of 2.) 21.4 SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 39 Fig | Authors and ISBNs for their books in ascending order by LastName and FirstName. (Part 2 of 2) 21.4 SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved INSERT Statement The INSERT statement inserts a row into a table: INSERT INTO tableName ( columnName1, columnName2, …, columnNameN ) VALUES ( value1, value2, …, valueN ) The SQL keyword VALUES specifies values in the new row. The values must match up with the columns specified after the table name in both order and type SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 41 The following INSERT statement inserts a row into the Authors table: INSERT INTO Authors ( FirstName, LastName ) VALUES ( 'Sue', 'Smith' ) AuthorID is an identity column, so it is assigned the next value in an autoincremented sequence SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 42 Figure shows the Authors table after the INSERT operation. Fig | Table Authors after an INSERT operation SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 43 Common Programming Error 21.5 It is an error to specify a value for an identity column in an INSERT statement. Common Programming Error 21.6 To specify a string containing a single quote in a SQL statement, there must be two single quotes in the position where the single-quote character appears in the string (e.g., 'O''Malley' ) SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved UPDATE Statement An UPDATE statement modifies data in a table: UPDATE tableName SET columnName1 = value1, columnName2 = value2, …, columnNameN =valueN WHERE criteria The following UPDATE statement updates a row in the Authors table. UPDATE Authors SET LastName = 'Jones‘ WHERE LastName = 'Smith' AND FirstName = 'Sue' 21.4 SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 45 Fig | Table Authors after an UPDATE operation. Figure shows the Authors table after the UPDATE operation has taken place SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 46 Keyword AND is a logical operator that returns true if and only if both of its operands are true. SQL also provides other logical operators, such as OR and NOT SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved DELETE Statement A DELETE statement removes rows from a table: DELETE FROM tableName WHERE criteria The following DELETE statement deletes the row for Sue Jones. DELETE FROM Authors WHERE LastName = 'Jones' AND FirstName = 'Sue' 21.4 SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 48 Fig | Table Authors after a DELETE operation. Figure shows the Authors table after the DELETE operation has taken place SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 49 LINQ to SQL uses LINQ syntax to query databases. LINQ to SQL classes are automatically generated by the IDE’s LINQ to SQL Designer. The IDE creates a class for each table, with a property for each column in the table LINQ to SQL
2009 Pearson Education, Inc. All rights reserved. 50 A cache is a temporary store created for fast access to data. LINQ to SQL caches all row objects that it creates, making interacting with the database more efficient. This can reduce round trips to the database LINQ to SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved. 51 LINQ queries on an IQueryable object are processed together as a single SQL statement. If each query operator were handled separately, multiple round trips to the database would be needed. A DataContext class controls the flow of data between the program and the database. When cached objects have been changed, these changes are saved using the DataContext ’ s SubmitChanges method LINQ to SQL (Cont.)
2009 Pearson Education, Inc. All rights reserved LINQ to SQL: Extracting Information from a Database Creating LINQ to SQL Classes Create a new Windows Form s Application named DisplayTable. Change the name of the source file to DisplayTableForm.cs.
2009 Pearson Education, Inc. All rights reserved LINQ to SQL: Extracting Information from a Database (Cont.) Select Tools > Connect to Database…. If the Choose Data Source dialog appears, select Microsoft SQL Server Database File from the Data source: ListBox. Click Continue to open the Add Connection dialog. Click Browse… and choose Books.mdf. Error-Prevention Tip 21.1 SQL Server Express allows only one application at a time to access a database file. Ensure that no other program is using the database file before you attempt to add it to the project.
2009 Pearson Education, Inc. All rights reserved LINQ to SQL: Extracting Information from a Database (Cont.) Right click the project in the Solution Explorer and select Add > New Item… Select LINQ to SQL classes, name the new item Books.dbml and click the Add button. The Database Explorer window allows you navigate the structure of databases. Drag the Authors, Titles and AuthorISBN tables onto the Object Relational Designer and select Yes. Error-Prevention Tip 21.2 Be sure to save the file in the Object Relational Designer before trying to use the LINQ to SQL classes in code. The IDE does not generate the classes until you save the file.
2009 Pearson Education, Inc. All rights reserved LINQ to SQL: Extracting Information from a Database (Cont.) Creating Data Bindings Select Data > Add New Data Source… to display the Data Source Configuration Wizard. In the dialog, select Object and click Next >. Expand the tree view and select DisplayTable > DisplayTable > Author. Click Next > then Finish. The Authors table in the database is now a data source that can be used by the bindings.
2009 Pearson Education, Inc. All rights reserved LINQ to SQL: Extracting Information from a Database (Cont.) Open the Data Sources window by selecting Data > Show Data Sources. Open the DisplayTableForm in Design view. Click the Author node in the Data Sources window—it should change to a drop-down list. Ensure that the DataGridView option is selected.
2009 Pearson Education, Inc. All rights reserved LINQ to SQL: Extracting Information from a Database (Cont.) Drag the Author node from the Data Sources window to the DisplayTableForm. The IDE creates a DataGridView with the correct column names and a BindingNavigator.
2009 Pearson Education, Inc. All rights reserved LINQ to SQL: Extracting Information from a Database (Cont.) The BindingNavigator contains Button s for moving between entries, adding entries, deleting entries and saving changes to the database. A BindingSource transfers data between the data source and the data-bound controls on the Form. (Fig ).
2009 Pearson Education, Inc. All rights reserved LINQ to SQL: Extracting Information from a Database (Cont.) Fig | Component tray holds nonvisual components in Design view. Component tray
2009 Pearson Education, Inc. All rights reserved. 60 Outline DisplayTable Form.cs ( 1 of 3 ) Figure shows the code needed to move data back and forth between the database and GUI. Fig | Component tray holds nonvisual components in Design view. (Part 1 of 3. )
2009 Pearson Education, Inc. All rights reserved. 61 Outline DisplayTable Form.cs ( 2 of 3 ) A DataContext object allows the application to interact with the database. The BindingSource ’s DataSource property is set to the results of a LINQ query. LINQ is used to extract data from the Authors table in the database. Fig | Component tray holds nonvisual components in Design view. (Part 2 of 3. )
2009 Pearson Education, Inc. All rights reserved. 62 Outline DisplayTable Form.cs ( 3 of 3 ) First, all controls on the form are validated. EndEdit forces any pending changes to be saved. SubmitChanges stores any changes to the database. Fig | Component tray holds nonvisual components in Design view. (Part 3 of 3. )
2009 Pearson Education, Inc. All rights reserved LINQ to SQL: Extracting Information from a Database (Cont.) Use the Properties window to set the save button’s Enabled property to True. Saving the data back to the database is a three-step process: – First, all controls on the form are validated. – EndEdit forces any pending changes to be saved. – SubmitChanges stores any changes to the database.
2009 Pearson Education, Inc. All rights reserved LINQ to SQL: Extracting Information from a Database (Cont.) To persist changes between program executions, select the database in the Solution Explorer and set the Copy to Output Directory property to Copy if newer. Run the application to verify that it works.
2009 Pearson Education, Inc. All rights reserved More Complex LINQ Queries and Data Binding Create a new Windows Forms Application named DisplayQueryResult. Rename its C# file to DisplayQueryResultForm.cs. Add the Books database to the project and generate the LINQ to SQL classes.
2009 Pearson Education, Inc. All rights reserved More Complex LINQ Queries and Data Binding (Cont.) Create the data source and the DataGridView. Select the Title class as the data source, and drag the Title node from the Data Sources window onto the form. Leave the Form ’s Design view open and add a ComboBox named queriesComboBox below the DataGridView on the Form. Open the String Collection Editor by clicking the small arrowhead that appears in the upper-right corner of the control and selecting Edit Items.
2009 Pearson Education, Inc. All rights reserved More Complex LINQ Queries and Data Binding (Cont.) Add the following three items to queriesComboBox : – All titles – Titles with 2008 copyright – Titles ending with "How to Program"
2009 Pearson Education, Inc. All rights reserved. 68 Outline DisplayQuery ResultForm.cs ( 1 of 5) The application executes the appropriate query when the user selects an item (Fig ). Fig | Displaying the result of a user-selected query in a DataGridView. (Part 1 of 5. ) Declaring the BooksDataContext.
2009 Pearson Education, Inc. All rights reserved. 69 Outline DisplayQuery ResultForm.cs ( 2 of 5) Fig | Displaying the result of a user-selected query in a DataGridView. (Part 2 of 5. ) Enable the save Button so this event handler will execute. Setting the BooksDataContext ’s Log property, where all commands will be recorded.
2009 Pearson Education, Inc. All rights reserved. 70 Outline DisplayQuery ResultForm.cs ( 3 of 5) Fig | Displaying the result of a user-selected query in a DataGridView. (Part 3 of 5. ) Enable the save Button so this event handler will execute.
2009 Pearson Education, Inc. All rights reserved. 71 Outline DisplayQuery ResultForm.cs ( 4 of 5) Fig | Displaying the result of a user-selected query in a DataGridView. (Part 4 of 5. ) The MoveFirst method focuses the first element each time a query executes.
2009 Pearson Education, Inc. All rights reserved. 72 Outline DisplayQuery ResultForm.cs ( 5 of 5) Fig | Displaying the result of a user-selected query in a DataGridView. (Part 5 of 5. )
2009 Pearson Education, Inc. All rights reserved More Complex LINQ Queries and Data Binding (Cont.) The DataContext object is set to log all queries to Console.Out. The Output window can be opened by selecting View > Output in the IDE (Fig ). a) SQL generated by the All titles query. Fig | Output window of the Display Query Result application. (Part 1 of 2. )
2009 Pearson Education, Inc. All rights reserved More Complex LINQ Queries and Data Binding (Cont.) c) SQL generated by the Titles ending with "How to Program" query. b) SQL generated by the Titles with 2008 copyright query. Fig | Output window of the Display Query Result application. (Part 2 of 2. )
2009 Pearson Education, Inc. All rights reserved. 75 Outline JoiningTest.cs ( 1 of 7 ) Figure uses LINQ to SQL to combine and organize data from multiple tables. Fig | Using LINQ to perform a join and aggregate data across tables. (Part 1 of 7. )
2009 Pearson Education, Inc. All rights reserved. 76 Outline JoiningTest.cs ( 2 of 7 ) Using LINQ’s join clause to combine data from multiple tables. Using LINQ to SQL properties to access related rows in other tables. Fig | Using LINQ to perform a join and aggregate data across tables. (Part 2 of 7. )
2009 Pearson Education, Inc. All rights reserved. 77 Outline JoiningTest.cs ( 3 of 7 ) Using LINQ to return hierarchical results through a nested query in the second let clause. Fig | Using LINQ to perform a join and aggregate data across tables. (Part 3 of 7. ) Using LINQ to SQL properties to access related rows in other tables.
2009 Pearson Education, Inc. All rights reserved. 78 Outline JoiningTest.cs ( 4 of 7 ) Fig | Using LINQ to perform a join and aggregate data across tables. (Part 4 of 7. )
2009 Pearson Education, Inc. All rights reserved. 79 Outline JoiningTest.cs ( 5 of 7 ) Fig | Using LINQ to perform a join and aggregate data across tables. (Part 5 of 7. )
2009 Pearson Education, Inc. All rights reserved. 80 Outline JoiningTest.cs ( 6 of 7 ) Fig | Using LINQ to perform a join and aggregate data across tables. (Part 6 of 7. )
2009 Pearson Education, Inc. All rights reserved. 81 Outline JoiningTest.cs ( 7 of 7 ) Fig | Using LINQ to perform a join and aggregate data across tables. (Part 7 of 7. )
2009 Pearson Education, Inc. All rights reserved. 82 Outline MasterDetail Form.cs ( 1 of 6 ) Figure demonstrates a master/detail view—one part of the interface allows you to select an entry, and another part displays detailed information about that entry. Fig | Using a DataGridView to display details based on a selection. (Part 1 of 6.)
2009 Pearson Education, Inc. All rights reserved. 83 Outline MasterDetail Form.cs ( 2 of 6 ) Fig | Using a DataGridView to display details based on a selection. (Part 2 of 6.) The ComboBox ’s DisplayMember property is set to "Name". Creating an AuthorBinding object for each author as the ComboBox ’s DataSource. The text in the ComboBox is retrieved from the BookTitle property.
2009 Pearson Education, Inc. All rights reserved. 84 Outline MasterDetail Form.cs ( 3 of 6 ) Fig | Using a DataGridView to display details based on a selection. (Part 3 of 6.) Creating the DataSource for titleComboBox. Retrieving the selected Author and using LINQ to retrieve related Title s.
2009 Pearson Education, Inc. All rights reserved. 85 Outline MasterDetail Form.cs ( 4 of 6 ) Fig | Using a DataGridView to display details based on a selection. (Part 4 of 6.) Retrieving the selected Author and using LINQ to retrieve related Title s.
2009 Pearson Education, Inc. All rights reserved. 86 Outline a) Master/Detail application when it begins execution MasterDetail Form.cs ( 5 of 6 ) Fig | Using a DataGridView to display details based on a selection. (Part 5 of 6.)
2009 Pearson Education, Inc. All rights reserved. 87 Outline b) Select Paul Deitel from the Author: drop-down list to view books he has co-authored c) Select Simply Visual Basic 2008 from the Title: drop- down to view the authors who wrote that book MasterDetail Form.cs ( 6 of 6 ) Fig | Using a DataGridView to display details based on a selection. (Part 6 of 6.)
2009 Pearson Education, Inc. All rights reserved Creating a Master/Detail View Application Create a new Windows Form s Application called MasterDetail. Add the Books database and create the LINQ to SQL classes.
2009 Pearson Education, Inc. All rights reserved Creating a Master/Detail View Application (Cont.) Add two Labels and two ComboBoxes, positioned as shown in Fig Create a DataGridView called booksDataGridView and set its ReadOnly property to True using the Properties window. Fig | Finished design of MasterDetail application.
2009 Pearson Education, Inc. All rights reserved. 90 Outline AddressBookForm.cs ( 1 of 5 ) The AddressBook application (Fig ) provides a GUI for querying the database with LINQ. Fig | Manipulating an address book. (Part 1 of 5.)
2009 Pearson Education, Inc. All rights reserved. 91 Outline AddressBookForm.cs ( 2 of 5 ) Fig | Manipulating an address book. (Part 2 of 5.) The BindDefault method sets the AddressBinding Source ’s DataSource property to the result of a LINQ query.
2009 Pearson Education, Inc. All rights reserved. 92 Outline AddressBookForm.cs ( 3 of 5 ) Fig | Manipulating an address book. (Part 3 of 5.) Data is set to be displayed when the application starts. Event handler for the BindingNavigator ’s save Button.
2009 Pearson Education, Inc. All rights reserved. 93 Outline AddressBookForm.cs ( 4 of 5 ) Fig | Manipulating an address book. (Part 4 of 5.) Doing a search changes the DataSource to a set of matches.
2009 Pearson Education, Inc. All rights reserved. 94 Outline AddressBookForm.cs ( 5 of 5 ) Fig | Manipulating an address book. (Part 5 of 5.) a) AddressBook application after adding four entries.b) Searching for a specific last name. c) Use the Browse All Entries Button to view all people in the address book.
2009 Pearson Education, Inc. All rights reserved Programming with LINQ to SQL: Address-Book Case Study Create a new Windows Form s Application named AddressBook. Add the AddressBook.mdf database and name the file AddressBook.dbml. You must also add the Addresses table as a data source.
2009 Pearson Education, Inc. All rights reserved Programming with LINQ to SQL: Address-Book Case Study (Cont.) Click the Address node in the Data Sources window. Click the down arrow to view the items in the list. – Select the Details option to indicate that the IDE should create a set of Label/TextBox pairs. Drag the Address node from the Data Sources window to the Form.
2009 Pearson Education, Inc. All rights reserved Programming with LINQ to SQL: Address-Book Case Study (Cont.) The AddressID is an autoincremented identity column, so set its ReadOnly property to True. The database in this example is initially empty, so you’ll need to add several records before testing the find capability. To add search functionality, we create controls to allow the user to enter a last name. When you enter a last name and click Find, the BindingNavigator is restricted to matches because the data source is changed.
2009 Pearson Education, Inc. All rights reserved Programming with LINQ to SQL: Address-Book Case Study (Cont.) The IDE sets the TextBox’s Text with the DataBindings.Text property. Click the plus sign next to ( DataBindings ) in the Properties window. Clicking the drop-down list (as in Fig ) allows you to choose a BindingSource object and a property to bind.
2009 Pearson Education, Inc. All rights reserved Programming with LINQ to SQL: Address-Book Case Study (Cont.) Fig | Data bindings for firstNameTextBox in the AddressBook application.