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

Slides:



Advertisements
Similar presentations
Database Basics. What is Access? Database management system Computer-based equivalent of a manual database Makes it easy to organize and update information.
Advertisements

1 After completing this lesson, you will be able to: Create a database. Create a table using the Table Wizard. Create and modify a table in Design view.
Chapter 10 Database Applications Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
 Open the Paradise.exe file  Application displays records in the Books database  Allows the store manager to enter an author’s name (or part of a name)
Chapter 14.3 LINQ to SQL Programming in Visual Basic 2010: The Very Beginner’s Guide by Jim McKeown Databases – Part 3.
Chapter 18 - Data sources and datasets 1 Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
Access Lesson 2 Creating a Database
Visual Basic 2010 How to Program. © by Pearson Education, Inc. All Rights Reserved.2.
Visual Basic 2010 How to Program Reference: Instructor: Maysoon Bin Duwais slides Visual Basic 2010 how to program by Deitel © by Pearson Education,
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 1 Committed to Shaping the Next Generation of IT Experts. Chapter 2: Relational Databases.
Chapter 12: Using ADO.NET 2.0 Programming with Microsoft Visual Basic 2005, Third Edition.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
Chapter 12: ADO.NET and ASP.NET Programming with Microsoft Visual Basic.NET, Second Edition.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
Microsoft Visual Basic 2012 CHAPTER TEN Incorporating Databases with ADO.NET.
CSCI 3328 Object Oriented Programming in C# Chapter 12: Databases and LINQ 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Chapter 3 Maintaining a Database
An INTRODUCTION to MICROSOFT ACESS 2000 By Dr. Ennis-Cole.
Unit J: Creating a Database Microsoft Office Illustrated Fundamentals.
Databases and LINQ Visual Basic 2010 How to Program 1.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Introducing Web Controls Outline 29.1 Analyzing the.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Shipping Hub Application Introducing Generic Collections, LINQ, For Each...Next.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Typing Application Introducing Keyboard Events, Menus, Dialogs and the Dictionary.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 2 Welcome Application Introducing the Visual Basic 2008 Express Edition IDE.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 30 – Bookstore Application: Client Tier Examining.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Thirteen Working with Access Databases and LINQ.
McGraw-Hill/Irwin ©2009 The McGraw-Hill Companies, All Rights Reserved Business Driven Information Systems 2e Plug-In T6: Basic Skills and Tools Using.
Lesson 2.  To help ensure accurate data, rules that check entries against specified values can be applied to a field. A validation rule is applied to.
Connecting to Data Sources Using ADO.NET Dr. Awad Khalil Computer Science & Engineering Department AUC.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
1 By: Nour Hilal. Microsoft Access is a database software where data is stored in one or more Tables. A Database is a group of related Tables. Access.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
 2006 Pearson Education, Inc. All rights reserved Database, SQL and ADO.NET.
Key Applications Module Lesson 21 — Access Essentials
Chapter 17 Creating a Database.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 3 Welcome Application Introduction to Visual Programming.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Security Panel Application Introducing the Select Case Multiple-Selection Statement.
Course ILT Forms and queries Unit objectives Create forms by using AutoForm and the Form Wizard, and add or modify form headers and footers Open and enter.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
Databases with LINQ. LINQ to SQL LINQ to SQL uses LINQ syntax to query databases. LINQ to SQL classes are automatically generated by the IDE’s LINQ to.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 2.1 Test-Driving the Welcome Application 2.2.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Bookstore Application: Middle Tier Introducing Code-Behind Files, Session State.
1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases.
 2009 Pearson Education, Inc. All rights reserved Databases and LINQ to SQL.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 4 Designing the Inventory Application Introducing TextBox es and Button s.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Microsoft Office 2013 Try It! Chapter 4 Storing Data in Access.
Understand Databound Controls Windows Development Fundamentals LESSON 4.2A.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
VB.NET and Databases. ADO.NET VB.Net allows you many ways to connect to a database. The technology used to interact with a database or data source is.
IE 411/511: Visual Programming for Industrial Applications Lecture Notes #2 Introduction to the Visual Basic Express 2010 Integrated Development Environment.
Creating SQL Database file And Displaying a Database Table in a DataGridView.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Copyright © 2014 Pearson Canada Inc. Ext. 5b-1 Copyright © 2014 Pearson Canada Inc. Application Extension 5b Using Microsoft Access Part 2: Using Information.
Dive Into® Visual Basic 2010 Express
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Visual Basic 2010 How to Program
Introduction to Database Programs
Introduction to Database Programs
Presentation transcript:

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

 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

 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

 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

Application Requirements  2009 Pearson Education, Inc. All rights reserved Test-Driving the Address Book Application You have been asked to create an address book application that stores the first name, last name, 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.

 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

 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

 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

 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 TextBox and save the changes to this entry. (Fig. 24.4). Figure 24.4 | Editing an entry in the Address Book application. field updated

 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.

 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

 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

 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.)

 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

 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.)

 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.)

 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

 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

 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.)

 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

 2009 Pearson Education, Inc. All rights reserved. 21 ■Figure 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 | Addresses table data. Fields (columns) Adding a Database Connection to the Address Book Application (Cont.) Records (rows)

 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.)

 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

 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 ). Modeling the Database with LINQ to SQL Classes (Cont.) Figure | Adding LINQ to SQL classes. LINQ to SQL Classes template

 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 | 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

 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 ) by selecting Data > Show Data Sources. –Click the link Add New Data Source.... Adding a Data Source to the Address Book Application

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

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

 2009 Pearson Education, Inc. All rights reserved. 29 ■Expand the AddressBook node and select the Address class as in Figure 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 | Select Address to add a data source that can bind Address information to data-bound controls.

 2009 Pearson Education, Inc. All rights reserved. 30 ■An Address node now appears in the Data Sources window (Fig ). Figure | 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

 2009 Pearson Education, Inc. All rights reserved. 31 ■Open AddressBookForm in Design view, then click the Address node in the Data Sources window (Fig ). ■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 | Selecting a display format for the data. Default control for displaying data Select Details to display data in a set of Label–TextBox pairs

 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 ). –The IDE also creates a BindingNavigator and a BindingSource. Displaying the Address Fields on the Form (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 33 Figure | 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

 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.)

 2009 Pearson Education, Inc. All rights reserved. 35 Figure | 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 ).

 2009 Pearson Education, Inc. All rights reserved. 36 ■Line 3 (Fig ) 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 | Creating a DataContext object. Coding the Form ’s Load Event Handler

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

 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.

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

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

 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 ). Figure | 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

 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.)

 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 ). ■Place these controls in a GroupBox named findByLastNameGroupBox. Figure | 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

 2009 Pearson Education, Inc. All rights reserved. 44 ■Double click the Find Button to create its Click event handler (Fig ). ■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.)

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

 2009 Pearson Education, Inc. All rights reserved. 46 Figure | 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 ). Browse All Entries Button

 2009 Pearson Education, Inc. All rights reserved. 47 Figure | 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 ). Refill the AddressBindingSource with all address entries Jump to the first entry Clear the searchTextBox

 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.

 2009 Pearson Education, Inc. All rights reserved. 49 ■Figure 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

 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

 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

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