Presentation is loading. Please wait.

Presentation is loading. Please wait.

T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 24 Address Book Application Introducing Database Programming.

Similar presentations


Presentation on theme: "T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 24 Address Book Application Introducing Database Programming."— Presentation transcript:

1 T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 24 Address Book Application Introducing Database Programming

2  2009 Pearson Education, Inc. All rights reserved. 2 Outline 24.1 Test-Driving the Address Book Application 24.2 Planning the Address Book Application 24.3 Creating Database Connections 24.4 Programming the Address Book Application

3  2009 Pearson Education, Inc. All rights reserved. 3 In this tutorial you will learn: ■Connect to databases. ■Create LINQ to SQL classes. ■View the contents of a SQL Server Express database. ■Add database controls to Windows Form s. ■Use the Data Sources window. ■Use LINQ to query a database. ■Read information from and update information in databases. Objectives

4  2009 Pearson Education, Inc. All rights reserved. 4 ■Sequential-access files are inappropriate for large instant-access applications. ■Instant access is made possible by databases. – Individual database records can be accessed directly without sequentially searching. – Databases have been used in business applications for decades. ■LINQ greatly simplifies manipulating databases, which previously required the use of Structured Query Language (SQL). Introduction

5 Application Requirements  2009 Pearson Education, Inc. All rights reserved. 5 24.1 Test-Driving the Address Book Application You have been asked to create an address book application that stores the first name, last name, e-mail address and phone number of multiple people in a database table. Each entry should be stored as a different row in the table. The user should be able to navigate through the data, add rows, delete rows and save changes to the data. Specific entries should be retrievable by searching the data by last name.

6  2009 Pearson Education, Inc. All rights reserved. 6 Test-Driving the Address Book Application ■Run the completed application (Fig. 24.1). ■The BindingNavigator at the top of the Form is the strip of Button s below the window’s title bar. Figure 24.1 | Address Book application. BindingNavigator Find an entry by last name GroupBox Labels and TextBoxes display contact information Browse All Entries Button ReadOnly Address ID: TextBox

7  2009 Pearson Education, Inc. All rights reserved. 7 Test-Driving the Address Book Application (Cont.) ■Click the Move next, Move last, Move previous and Move first Button s (Fig. 24.2) to navigate through entries. Figure 24.2 | Navigating the entries in the Address Book application. Move first Button Move previous Button Move next Button Move last Button

8  2009 Pearson Education, Inc. All rights reserved. 8 ■Click the Add new Button (Fig. 24.3). –The Address ID: TextBox is automatically filled in with the value 0. –Fill in the data fields, then click the Save Data Button Test-Driving the Address Book Application (Cont.) Figure 24.3 | Adding an entry in the Address Book application. Save Data Button Add new Button 0 is placed in Address ID: TextBox

9  2009 Pearson Education, Inc. All rights reserved. 9 Test-Driving the Address Book Application (Cont.) ■Navigate to the entry for Lisa Black (the first entry). ■Change the text in the Email: TextBox and save the changes to this entry. (Fig. 24.4). Figure 24.4 | Editing an entry in the Address Book application. Email: field updated

10  2009 Pearson Education, Inc. All rights reserved. 10 ■Enter Brown in the TextBox of the Find an entry by last name GroupBox, then click the Find Button (Fig. 24.5). ■Note that the BindingNavigator now shows 1 of 2, because there are only two entries with the last name Brown. Test-Driving the Address Book Application (Cont.) Figure 24.5 | Browsing entries by last name.

11  2009 Pearson Education, Inc. All rights reserved. 11 Test-Driving the Address Book Application (Cont.) ■Navigate to an entry and click the Delete Button (Fig. 24.6). ■The BindingNavigator now displays 4 of 6 instead of 4 of 7. Figure 24.6 | Deleting an entry in the Address Book application. Click the Delete Button

12  2009 Pearson Education, Inc. All rights reserved. 12 When the Form loads Display the first entry in the AddressBook database When the user clicks the BindingNavigator’s auto-generated Add new Button Add a new entry When the user clicks the BindingNavigator’s auto-generated Save Data Button Update the database with any new, deleted or updated entries 24.2 Planning the Address Book Application

13  2009 Pearson Education, Inc. All rights reserved. 13 When the user clicks the BindingNavigator’s auto-generated Delete Button Delete the current entry displayed in the Form When the user clicks the Browse All Entries Button Display the first entry in the database and allow the user to browse all entries with the BindingNavigator Clear the search text box When the user clicks the Find Button If no entries have a last name that matches the input string, then display empty TextBoxes Otherwise, display the first entry with the specified last name and allow the user to browse through all matching entries with the BindingNavigator 24.2 Planning the Address Book Application (Cont.)

14  2009 Pearson Education, Inc. All rights reserved. 14 ■Use an ACE table to convert pseudocode into Visual Basic (Fig. 24.7). Figure 24.7 | ACE table for the Address Book application. (Part 1 of 3.) Action/Control/Event (ACE) Table for the Address Book Application

15  2009 Pearson Education, Inc. All rights reserved. 15 Figure 24.7 | ACE table for the Address Book application. (Part 2 of 3.) Action/Control/Event (ACE) Table for the Address Book Application (Cont.)

16  2009 Pearson Education, Inc. All rights reserved. 16 Figure 24.7 | ACE table for the Address Book application. (Part 3 of 3.) Action/Control/Event (ACE) Table for the Address Book Application (Cont.)

17  2009 Pearson Education, Inc. All rights reserved. 17 ■Create a new Windows Forms Application named AddressBook. ■Change the name of the source file to AddressBook.vb and change the Form ’s Name to AddressBookForm. ■Change the Form ’s Font to 9pt Segoe UI. Then set the Form ’s Text property to Address Book. Adding a Database Connection to the Address Book Application

18  2009 Pearson Education, Inc. All rights reserved. 18 ■Select Tools > Connect to Database... –Select Microsoft SQL Server Database File. –Click Continue to open the Add Connection dialog. ■Click Browse..., and open the AddressBook.mdf database file. Adding a Database Connection to the Address Book Application (Cont.) Figure 24.8 | Adding a database with the Add Connection dialog. Data source type Location of database file

19  2009 Pearson Education, Inc. All rights reserved. 19 ■Select View > Database Explorer. ■A database table stores related information in rows and columns. ■To view the contents of the Addresses table: –Expand the AddressBook.mdf node in the Database Explorer, then expand the Tables node. –Right click Addresses, and select Show Table Data (Fig. 24.9). Adding a Database Connection to the Address Book Application (Cont.)

20  2009 Pearson Education, Inc. All rights reserved. 20 Figure 24.9 | Viewing the Addresses table. Adding a Database Connection to the Address Book Application (Cont.) Click to display the database’s tables Right click the Addresses node Select to view the table’s contents

21  2009 Pearson Education, Inc. All rights reserved. 21 ■Figure 24.10 displays the contents of the Addresses table used in the Address Book application. ■A record is a table row, and a field is a table column. Figure 24.10 | Addresses table data. Fields (columns) Adding a Database Connection to the Address Book Application (Cont.) Records (rows)

22  2009 Pearson Education, Inc. All rights reserved. 22 ■A table should contain a primary key. –This is a field containing unique values to distinguish records from one another. –In this table, the AddressID field is the primary key. ■Right click the Addresses tab in the IDE and select Close. Adding a Database Connection to the Address Book Application (Cont.)

23  2009 Pearson Education, Inc. All rights reserved. 23 ■LINQ to SQL classes create an in-memory model of your application’s database. –These classes use ADO.NET technologies to retrieve information from and send information to the database. –LINQ to SQL classes manage all the ADO.NET code behind the scenes. Modeling the Database with LINQ to SQL Classes

24  2009 Pearson Education, Inc. All rights reserved. 24 ■Right click the AddressBook project in the Solution Explorer and select Add > New Item.... –In the Add New Item dialog, select LINQ to SQL Classes and add AddressBookDataClasses.dbml (Fig. 24.11). Modeling the Database with LINQ to SQL Classes (Cont.) Figure 24.11 | Adding LINQ to SQL classes. LINQ to SQL Classes template

25  2009 Pearson Education, Inc. All rights reserved. 25 ■In the Object Relational Designer expand the AddressBook.mdf node in the Database Explorer. –Expand the Tables node. –Drag the Addresses table onto the left pane (Fig 24.12). –Click Yes in the dialog prompt. Modeling the Database with LINQ to SQL Classes (Cont.) Figure 24.12 | Adding a table from the Database Explorer to the Object Relational Designer. Address class created from the Addresses table Properties representing columns in the Addresses

26  2009 Pearson Education, Inc. All rights reserved. 26 ■A data-bound control displays information contained in a data source. –The BindingNavigator and TextBox es in the completed application are data-bound controls. ■Open the Data Sources window (Fig. 24.13) by selecting Data > Show Data Sources. –Click the link Add New Data Source.... Adding a Data Source to the Address Book Application

27  2009 Pearson Education, Inc. All rights reserved. 27 Figure 24.13 | Data Sources window. Add New Data Source Button Add New Data Source link Adding a Data Source to the Address Book Application (Cont.)

28  2009 Pearson Education, Inc. All rights reserved. 28 ■This opens the Data Source Configuration Wizard. ■Select Object in the wizard (Fig. 24.14) and click Next >. Figure 24.14 | Data Source Configuration Wizard dialog. Select Object as the data source type Adding a Data Source to the Address Book Application (Cont.)

29  2009 Pearson Education, Inc. All rights reserved. 29 ■Expand the AddressBook node and select the Address class as in Figure 24.15 and click Finish. –If the Address class is not displayed, select File > Save All. Adding a Data Source to the Address Book Application (Cont.) Select the Address class as the object data source Figure 24.15 | Select Address to add a data source that can bind Address information to data-bound controls.

30  2009 Pearson Education, Inc. All rights reserved. 30 ■An Address node now appears in the Data Sources window (Fig. 24.16). Figure 24.16 | Updated Data Sources window. Data source name Adding a Data Source to the Address Book Application (Cont.) Addresses table’s fields represented in the data source

31  2009 Pearson Education, Inc. All rights reserved. 31 ■Open AddressBookForm in Design view, then click the Address node in the Data Sources window (Fig. 24.17). ■Click the down arrow to view the items in the list. –Select the Details option in the drop-down list to indicate that the IDE should create a set of Label–TextBox pairs for each field. Displaying the Address Fields on the Form Address data source Figure 24.17 | Selecting a display format for the data. Default control for displaying data Select Details to display data in a set of Label–TextBox pairs

32  2009 Pearson Education, Inc. All rights reserved. 32 ■Drag the Address node from the Data Sources window to the Form. –The IDE creates a series of Label s and TextBoxes (Fig. 24.18). –The IDE also creates a BindingNavigator and a BindingSource. Displaying the Address Fields on the Form (Cont.)

33  2009 Pearson Education, Inc. All rights reserved. 33 Figure 24.18 | Displaying a table on a Form using a series of Label s and TextBox es. Auto-generated BindingNavigator Auto-generated data binding objects Displaying the Address Fields on the Form (Cont.) Auto-generated Labels and TextBoxes to display contact information

34  2009 Pearson Education, Inc. All rights reserved. 34 ■The AddressID is used to uniquely identify each record, so users should not be allowed to edit its value. –Set the Address ID: TextBox ’s ReadOnly property to True. –Also set its TabStop property to False. Displaying the Address Fields on the Form (Cont.)

35  2009 Pearson Education, Inc. All rights reserved. 35 Figure 24.19 | GUI controls repositioned. Displaying the Address Fields on the Form (Cont.) Labels and TextBoxes repositioned ■Reposition the GUI controls to place them in a more natural order, then modify the application’s tab order (Fig. 24.19).

36  2009 Pearson Education, Inc. All rights reserved. 36 ■Line 3 (Fig. 24.20) refers to AddressBookDataClassesDataContext — defined as part of the LINQ to SQL classes. ■This object has properties representing each table you added to the AddressBookDataClasses LINQ to SQL classes. Figure 24.20 | Creating a DataContext object. Coding the Form ’s Load Event Handler

37  2009 Pearson Education, Inc. All rights reserved. 37 ■The FillAll method (Fig. 24.21) defines a LINQ query that retrieves each entry in the Addresses table. Coding the Form ’s Load Event Handler (Cont.) Figure 24.21 | Retrieving data from the database using LINQ.

38  2009 Pearson Education, Inc. All rights reserved. 38 Coding the Form ’s Load Event Handler (Cont.) ■The From clause specifies the data source. ■The AddressBookDataClassesDataContext object handles all the details of querying the database. ■The LINQ clause Order By sorts the query results according to the property specified after the Order By keywords.

39  2009 Pearson Education, Inc. All rights reserved. 39 ■Double click the Form to generate its event handler (Fig. 24.22). Figure 24.22 | Retrieving data from the database when the Form loads. Coding the Form ’s Load Event Handler (Cont.)

40  2009 Pearson Education, Inc. All rights reserved. 40 ■Run the application (Fig. 24.23). ■As you browse the entries, notice that they are sorted alphabetically by last name. ■The Save Data Button in the BindingNavigator is disabled. Figure 24.23 | Browsing entries in the Address Book application. Coding the Form ’s Load Event Handler (Cont.) Save Data button disabled

41  2009 Pearson Education, Inc. All rights reserved. 41 ■Set the Save Data Button ’s Enabled property to True. ■Double click the Save Data Button to create its Click event handler (Fig. 24.24). Figure 24.24 | Click event handler for the Save Data Button. Enabling the BindingNavigato r’s Save Data Button Validate the Form’s input controls Save changes to the BindingSource Update the database on disk

42  2009 Pearson Education, Inc. All rights reserved. 42 ■The Validate method validates any of the controls on the Form that implement Validating or Validated events. ■The EndEdit method ensures that the data source is updated with any changes made by the user. ■The SubmitChanges method writes the changes to the SQL Server database on disk. ■The BindingSource ’s MoveFirst method to move to the first entry. Enabling the BindingNavigato r’s Save Data Button (Cont.)

43  2009 Pearson Education, Inc. All rights reserved. 43 ■Add to the Form a Label named searchLabel, a TextBox named searchTextBox and a Button named findButton (Fig. 24.25). ■Place these controls in a GroupBox named findByLastNameGroupBox. Figure 24.25 | Add controls to search data by last name. searchTextBox searchLabel Find an entry by last name GroupBox Searching the LastName Field in the AddressBook.mdf Database findButton

44  2009 Pearson Education, Inc. All rights reserved. 44 ■Double click the Find Button to create its Click event handler (Fig. 24.26). ■A LINQ query retrieves entries where the last name matches the name specified in searchTextBox. ■Because all entries returned by this query have the same last name, the results are ordered the by first name. Searching the LastName Field in the AddressBook.mdf Database (Cont.)

45  2009 Pearson Education, Inc. All rights reserved. 45 Figure 24.26 | Retrieving entries with the specified last name. Searching the LastName Field in the AddressBook.mdf Database (Cont.)

46  2009 Pearson Education, Inc. All rights reserved. 46 Figure 24.27 | Adding the Browse All Entries Button to the Form. Adding the Browse All Entries Button and Its Click Event Handler ■Add a Button named browseAllButton and set its Text property to Browse All Entries (Fig. 24.27). Browse All Entries Button

47  2009 Pearson Education, Inc. All rights reserved. 47 Figure 24.28 | Click event handler for browseAllButton. Adding the Browse All Entries Button and Its Click Event Handler (Cont.) ■Double click the Browse All Entries Button to create its Click event handler (Fig. 24.28). Refill the AddressBindingSource with all address entries Jump to the first entry Clear the searchTextBox

48  2009 Pearson Education, Inc. All rights reserved. 48 Testing Your Completed Address Book Application ■Enter a new contact, fill in the fields, then click the Save Data Button. ■Next, search for the last name of the contact that you entered, using searchTextBox and findButton. ■Now click the Browse All Entries Button. ■Test the delete function by deleting an entry and then clicking the Save Data Button.

49  2009 Pearson Education, Inc. All rights reserved. 49 ■Figure 24.29 presents the source code for the application. Outline (1 of 4 ) DataContext class used to interact with the database Query the Addresses table Order results by LastName Fill the BindingSource with data

50  2009 Pearson Education, Inc. All rights reserved. 50 Outline (2 of 4 ) Indicate edits are complete Update the database Validate the Form’s controls Move to the first item in the results from the database

51  2009 Pearson Education, Inc. All rights reserved. 51 Outline (3 of 4 ) Fill the BindingSource with data Query the Addresses table Entries with the specified LastName Order results by FirstName

52  2009 Pearson Education, Inc. All rights reserved. 52 Outline (4 of 4 )


Download ppt "T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 24 Address Book Application Introducing Database Programming."

Similar presentations


Ads by Google