Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2.

Similar presentations


Presentation on theme: "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2."— Presentation transcript:

1 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2 Planning the ATM Application 25.3 Creating Database Connections 25.4 Programming the ATM Application 25.5 Wrap-Up Tutorial 25 - ATM Application Introducing Database Programming

2 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Connect to databases. –View the contents of an Access database. –Add database controls to Windows Form s. –Use the Server Explorer window. –Use the Query Builder dialog. –Read information from and update information in databases.

3 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3 25.1 Test-Driving the ATM Application

4 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 25.1 Test-Driving the ATM Application The ATM Application –Open ATM application and select Debug > Start to run –OK, Balance, Withdraw, and Done Button s are disabled –After account number is entered, user is prompted for a pin –Characters in PIN display as asterisks and enable OK Button –Clicking the OK Button displays a welcome message and enables the Balance and Withdraw Button s –Clicking the Balance Button displays the current account balance –Clicking Withdraw prompts the user to enter a monetary amount and displays the new balance after the withdrawal

5 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 25.1 Test-Driving the ATM Application Figure 25.1 ATM application Form. Displays instructions and messages to the user Keypad for entering PIN and withdrawal amount ComboBox that displays account numbers Disabled Button s

6 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 25.1 Test-Driving the ATM Application Figure 25.2 Providing PIN for the selected account. Asterisk is displayed here for each keypad Button pressed for the PIN Account number selected

7 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7 25.1 Test-Driving the ATM Application Figure 25.3 ATM displaying welcome message. Welcome message displays in Label when user enters correct PIN

8 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8 25.1 Test-Driving the ATM Application Figure 25.4 ATM application displaying balance after withdrawal. New balance displays in Label after user performs a withdrawal

9 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9 25.2 Planning the ATM Application Figure 25.5 ACE table for the ATM application.

10 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 25.2 Planning the ATM Application Figure 25.5 ACE table for the ATM application.

11 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 25.2 Planning the ATM Application

12 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12 25.3 Creating Database Connections Adding a database –View > Server Explorer to display Server Explorer window –Create a connection object to maintain a connection to a database

13 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13 25.3 Creating Database Connections Figure 25.6 Server Explorer window. Click the Connect to Database Button

14 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14 25.3 Creating Database Connections Figure 25.7 Provider tab in the Data Link Properties dialog. Provider tab Select this providerClick to continue

15 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 25.3 Creating Database Connections Figure 25.8 Connection tab of the Data Link Properties dialog. Connection tab Click to select a database

16 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16 25.3 Creating Database Connections Figure 25.9 Select Access Database dialog. Select the db_ATM.mdb file – db_ATM.mdb contains data you will query

17 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17 25.3 Creating Database Connections Figure 25.10 Connection tab containing selected database path. Database path displayed User name should be Admin Blank password option should be checked Click to test the connection

18 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 18 25.3 Creating Database Connections Figure 25.11 Database connection shown in the Server Explorer window. Click to expandData connection Drag and drop the data connection on the Form to create an OleDbConnection object

19 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19 25.3 Creating Database Connections Figure 25.12 Storing the database’s password. – Database has no password, click Don’t include password Button.

20 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 20 25.3 Creating Database Connections Figure 25.13 OleDbConnection object added to the Form. OleDbConnection object added to the Form

21 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 21 25.3 Creating Database Connections Understanding the db_ATM.mdb database structure –AccountInformation is a table in the db_ATM.mdb database –Tables store information in rows and columns in a database –In AccountInformation, the ten records are in table rows and the four fields are table columns –Primary keys are columns that distinguish rows from one another –This database’s primary key is the AccountNumber column –The value of the primary key in each row of the table must be unique

22 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 22 25.3 Creating Database Connections Figure 25.14 Viewing the AccountInformation table. Click to display the Tables node Right click the AccountInformation node Select to view the table’s contents

23 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 23 25.3 Creating Database Connections Figure 25.15 AccountInformation table’s content. Rows (records)Columns (fields) Collectively, rows and columns form a table

24 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 24 25.3 Creating Database Connections Data Command Objects –Data command objects retrieve and update data in the database –To create a data command object, drag-and-drop an ADO.NET OleDbCommand control from the Data tab in the Toolbox to the Form. –Query Builder allows you to specify the commands that retrieve information from and modify information in databases

25 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25 25.3 Creating Database Connections Figure 25.16 Properties of objSelectAccount. Click to specify connection object Select the existing connection ( objOleDbConnection ) Connection property

26 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26 25.3 Creating Database Connections Figure 25.17 Query Builder and Add Table dialogs. Add Table dialog Select this table Click to add selected table Close Button

27 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27 25.3 Creating Database Connections Figure 25.18 AccountInformation table added to the Query Builder dialog. AccountInformation window appears once AccountInformation is added in the Add Table dialog Primary key displayed in bold Partial SQL query

28 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 28 25.3 Creating Database Connections Data Command Objects (continued) –Words SELECT and FROM represent SQL code –Structured Query Language (SQL) often performs database queries and manipulates data. –Criteria column specifies a specific row to be retrieved or manipulated –In the Criteria column, type =? Adds WHERE to end of SQL code –Specifies criteria that determine rows to retrieve –Funnel icon denotes a value in the Criteria column

29 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 29 25.3 Creating Database Connections Figure 25.19 Selecting columns in the Query Builder dialog. Check this CheckBox Names selected in the AccountInformation window appear here SQL SELECT statement

30 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 30 25.3 Creating Database Connections Figure 25.20 WHERE SQL keyword added to the SQL statement. Check this CheckBox Uncheck this box This value will be removed when AccountNumber ’s Output CheckBox is unchecked Funnel icon Criteria column Type this value

31 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 31 25.3 Creating Database Connections Figure 25.21 Complete SQL SELECT statement. AccountNumber CheckBox is now unchecked Complete SELECT statement

32 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 32 25.3 Creating Database Connections Figure 25.22 Microsoft Development Environment dialog.

33 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 33 25.3 Creating Database Connections Figure 25.23 Parameters property of a data command object. Parameters property Click to view collection

34 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 34 25.3 Creating Database Connections AccountNumber parameter Parameter name which you will use in C# code to complete the SQL statement Figure 25.24 OleDbParameter Collection Editor dialog.

35 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 35 25.3 Creating Database Connections Figure 25.25 OleDbCommands displayed in the component tray. OleDbCommand object

36 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 36 25.3 Creating Database Connections Figure 25.26 Specifying an UPDATE command. Right click the Column Select this to create an UPDATE statement

37 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 37 25.3 Creating Database Connections Figure 25.27 Specifying the BalanceAmount column in the UPDATE command. Check this CheckBox New Value column Type in this value This expression appears

38 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 38 25.3 Creating Database Connections Figure 25.28 UPDATE command displayed in the Query Builder dialog. Complete UPDATE command with parameters

39 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 39 25.4 Programming the ATM Application Figure 25.29 System.Data.OleDb used by ATM.cs. Using namespace System.Data.OleDb

40 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 40 25.4 Programming the ATM Application Figure 25.30 Rearranging and commenting the control declarations.

41 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 41 25.4 Programming the ATM Application Displaying existing account numbers in the ComboBox –OleDbDataReader is a data reader object, or an object that reads data from a database –Data readers cannot modify data

42 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 42 25.4 Programming the ATM Application Figure 25.31 Opening the connection to the database and creating a data reader.

43 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 43 25.4 Programming the ATM Application Figure 25.32 Filling the ComboBox with account numbers. Read method begins to read information from the database and returns false if there are no more rows to be read

44 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 44 25.4 Programming the ATM Application Figure 25.33 Specifying the AccountNumber parameter value of the data command object and connecting to the database. Opening the database connection Setting the AccountNumber parameter value of the command object

45 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 45 25.4 Programming the ATM Application Figure 25.34 OleDbDataReader for reading a row’s data. Creating the data reader Use data reader to read from database Setting instance variable values to values retrieved from the database Closing the reader Closing the database connection

46 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 46 25.4 Programming the ATM Application Figure 25.35 Connecting to the database to update a column. Specifying objUpdateBalance ‘s BalanceAmount parameter value Specifying objUpdateBalance ’s ORIGINAL_AccountNumber parameter value

47 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 47 25.4 Programming the ATM Application Figure 25.36 Executing an UPDATE command before closing the database connection. Opening the database connection Executing the UPDATE command Closing the database connection Update command executed by calling ExecuteNonQuery from the objUpdateBalance object.

48 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 48 ATM.cs Part (1 of 18) Using System.Data.OleDb namespace

49 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 49 ATM.cs Part (2 of 18)

50 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 50 ATM.cs Part (3 of 18)

51 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 51 ATM.cs Part (4 of 18)

52 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 52 ATM.cs Part (5 of 18)

53 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 53 ATM.cs Part (6 of 18)

54 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 54 ATM.cs Part (7 of 18)

55 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 55 ATM.cs Part (8 of 18)

56 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 56 ATM.cs Part (9 of 18)

57 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 57 ATM.cs Part (10 of 18)

58 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 58 ATM.cs Part (11 of 18)

59 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 59 ATM.cs Part (12 of 18)

60 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 60 ATM.cs Part (13 of 18)

61 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 61 ATM.cs Part (14 of 18)

62 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 62 ATM.cs Part (15 of 18) Opens database connection

63 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 63 ATM.cs Part (16 of 18) Creates data reader Fills ComboBox with account numbers Closes database connection Setting the AccoutNumber parameter of objSelectAccount Data Opens database connection

64 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 64 ATM.cs Part (17 of 18) Creates data reader Reads with data reader Sets instance variables to read data from database Closes data readerCloses database connection Sets BalanceAmount parameter of objUpdateBalance

65 Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 65 ATM.cs Part (18 of 18) Sets parameter of objUpdateBalance Opens database connection Executes UPDATE command Closes database connection


Download ppt "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2."

Similar presentations


Ads by Google