Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic 2010 5 th Edition.

Similar presentations


Presentation on theme: "Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic 2010 5 th Edition."— Presentation transcript:

1 Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic 2010 5 th Edition

2 Programming with Microsoft Visual Basic 2010, 5 th Edition Previewing the Academy Award Winners Application Open the Award.exe file Application displays records from the Movies database The movies database stores information on Academy Award winners for Best Picture Movie title Year Name of production company Application allows user to add or delete database records GUI 2 程式的使用測試 下載 Chap14\Award.exe 、 Movies.accdb 在第 797~798 頁

3 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-1 Academy Award Winners application 3

4 Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson A Objectives After studying Lesson A, you should be able to: Add records to a dataset Delete records from a dataset Sort the records in a dataset 4

5 Programming with Microsoft Visual Basic 2010, 5 th Edition Adding Records to a Dataset The database is stored in Movies.accdb file Contains one table, tblMovies Contains nine records, each with three fields YearWon field is numeric Title field contains text ProductionCo field contains text 5 Figure 14-3 Data contained in the tblMovies table

6 Programming with Microsoft Visual Basic 2010, 5 th Edition Database Related Components in Academy Award Winners application Figure 14-4 Interface for the Academy Award Winners application 6

7 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-5 Records displayed in the TblMoviesDataGridView control 7 I/O VB Controls in Academy Award Winners application

8 Programming with Microsoft Visual Basic 2010, 5 th Edition Adding Records to a Dataset (cont’d.) Figure 14-6 Syntax and examples of adding a record to a dataset 8 same

9 Programming with Microsoft Visual Basic 2010, 5 th Edition Update Database After Adding Records to a Dataset TableAdapter object’s Update method Used to save the changes to the database associated with the dataset Good practice: Place Update method within the Try block of a Try….Catch statement Because errors can occur when saving data 9 syntax & example

10 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-8 Syntax and examples of saving dataset changes to a database (continues) 10 mechanism component tray

11 Programming with Microsoft Visual Basic 2010, 5 th Edition 11 Figure 13-16 Illustration of the relationships among the database, the objects in the component tray, and the bound controls in hard disk in memory on GUI

12 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-9 Add button’s Click event procedure 12 實作練習 下載 Chap14\Academy Award Solution 在第 799~804 頁 add a record to dataset update database GUI & controls component

13 Programming with Microsoft Visual Basic 2010, 5 th Edition Sorting the Records in a Dataset BindingSource object’s Sort method Used to sort records To have records sorted when application is started: Place Sort method in the form’s Load event procedure 13 Figure 14-11 Syntax and examples of sorting the records in a dataset mechanism

14 Programming with Microsoft Visual Basic 2010, 5 th Edition Deleting Records from a Dataset Code Delete button’s Click event procedure Deletes record whose YearWon field contains the value entered in the txtDeleteYear control Before deleting record, display a message that asks user for confirmation Use MessageBox.Show method 14 Figure 14-13 MessageBox.Show method entered in the btnDelete control’s Click event procedure GUI & controls

15 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-14 Syntax and examples of locating a record in a dataset 15

16 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-15 Syntax and an example of deleting a record from a dataset 16 實作練習程式碼

17 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-16 Additional code entered in the btnDelete control’s Click event procedure 17 實作練習 第 806~810 頁 GUI & controls

18 Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson A Summary To add a record to a dataset, use the syntax: dataSetName.tableName.AddtableNameRow(valueField1[, valueField2…, valueFieldN]) Use the TableAdapter object’s Update method to save dataset changes to a database Use the BindingSource object’s Sort method to sort the records in a dataset To locate a record in a dataset, use the syntax: dataRowVariable=dataSetName.tableName.FindByfieldNam e(value) Use the DataRow variable’s Delete method to delete a record from a dataset 18

19 Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson B Objectives After studying Lesson B, you should be able to: Query a database using the SQL SELECT statement Create queries using the Query Builder dialog box 19

20 Programming with Microsoft Visual Basic 2010, 5 th Edition Structured Query Language SQL (Structured Query Language) A set of statements to perform common database tasks Examples: Storing, retrieving, updating, deleting, and sorting data Can be used with various database management systems and computers The SELECT Statement The most commonly used statement in SQL Allows you to specify fields and records you want to view Can control the order in which records appear when displayed 20 syntax & example

21 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-20 Syntax and examples of the SELECT statement (continues) 21

22 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-20 Syntax and examples of the SELECT statement (cont’d.) 22

23 Programming with Microsoft Visual Basic 2010, 5 th Edition Creating a Query Right click MoviesDataSet.xsd to open the DataSet Designer window Right click tblMoviesTableAdapter to point to Add to open the shortcut menu and click Query Choose SQL statements as the Command type Choose a Query Type Two ways to build a query using the SELECT statement Type the statement yourself Or, use the Query Builder button Opens the Query Builder dialog box 23 實作練習 下載 Chap14\ Movie Awards Solution-SQL 在第 817~822 頁 DataSet Designer window

24 Programming with Microsoft Visual Basic 2010, 5 th Edition DataSet Designer window Figure 14-22 DataSet Designer window 24

25 Programming with Microsoft Visual Basic 2010, 5 th Edition TableAdapter Query Configuration Wizard Figure 14-23 Choose a Command Type screen in the TableAdapter Query Configuration Wizard 25 relationships among database, dataset & controls

26 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-24 Choose a Query Type screen 26 TableAdapter Query Configuration Wizard (Choose a Query Type screen)

27 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-25 Specify a SQL SELECT statement screen 27 Type the statement yourself here Opens the Query Builder dialog box here

28 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-26 Query Builder dialog box 28 對應

29 Programming with Microsoft Visual Basic 2010, 5 th Edition Run a Query in Query Builder dialog Click Execute Query button to run a query Results appear in the Results pane Create query to select all fields for records from year 2006 and later 29

30 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-28 SELECT statement containing a WHERE clause 30 Click to run a query

31 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-30 Records displayed in ascending order by the Title field 31

32 Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson B Summary The SELECT statement is used to query a database using SQL The SELECT statement’s WHERE clause limits the records displayed The SELECT statement’s ORDER BY clause sorts the selected records in a specified order To open the DataSet Designer window: Open the dataset’s schema file (.xsd) 32

33 Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson B Summary (cont’d.) To start the TableAdapter Query Configuration Wizard: Right-click table adapter’s name in DataSet Designer window Point to add on shortcut menu and click Query To open the Query Builder dialog box: Use TableAdapter Query Configuration Wizard Specify a SQL SELECT statement screen Then click Query Builder button The % wildcard is used to represent characters in the WHERE clause’s condition 33

34 Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson C Objectives After studying Lesson C, you should be able to: Create a parameter query Save a query Invoke a query from code Add records to a dataset using the SQL INSERT statement Delete records from a dataset using the SQL DELETE statement 34

35 Programming with Microsoft Visual Basic 2010, 5 th Edition Parameter Queries Parameter marker A question mark (?) Used in a parameter query Allows user to specify query parameters 35

36 Programming with Microsoft Visual Basic 2010, 5 th Edition Parameter Queries (cont’d.) Figure 14-32 Examples of parameter queries 36

37 Programming with Microsoft Visual Basic 2010, 5 th Edition Parameter Queries (cont’d.) Test SELECT statements from Figure 14-32 Right-click MoviesDataSet.xsd Then click Open to open DataSet Designer window Start the TableAdapter Query Configuration Wizard Use Query Builder dialog to create a query that selects only the Chicago record Execute query Type Chicago in the Value column of the Query Parameters dialog box 37

38 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-34 Query Parameters dialog box 38

39 Programming with Microsoft Visual Basic 2010, 5 th Edition Saving a Query For an application to use a query during run time: Must save query and invoke it from code Use the TableAdapter Query Configuration Wizard Save a query that contains SELECT statement Associate the query with one or more methods 39

40 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-36 Default query in the Specify a SQL SELECT statement screen Figure 14-37 Parameter query in the Specify a SQL SELECT statement screen 40

41 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-38 Completed Choose Methods to Generate screen 41

42 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-39 Wizard Results screen Figure 14-40 Method names included in the DataSet Designer window 42

43 Programming with Microsoft Visual Basic 2010, 5 th Edition Invoking a Query from Code Methods associated with a query Can be used to invoke query during run time Next example Enter appropriate methods in Display button’s Click event procedure 43

44 Programming with Microsoft Visual Basic 2010, 5 th Edition Invoking a Query from Code (cont’d.) Figure 14-41 If clause and Fill method entered in the procedure 44

45 Programming with Microsoft Visual Basic 2010, 5 th Edition Invoking a Query from Code (cont’d.) Figure 14-43 Display button’s Click event procedure 45

46 Programming with Microsoft Visual Basic 2010, 5 th Edition The INSERT and DELETE Statements INSERT statement Used to insert records in a database DELETE statement Used to delete records from a database Both statements can be used in a query Known as Insert query and Delete query 46

47 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-45 Syntax and examples of the SQL INSERT statement 47

48 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-46 Syntax and examples of the SQL DELETE statement 48

49 Programming with Microsoft Visual Basic 2010, 5 th Edition The INSERT and DELETE Statements (cont’d.) Create an insert query Start the TableAdapter Query Configuration Wizard Verify Use SQL statements radio button is selected Click INSERT radio button On Choose a Query Type screen Default INSERT statement appears Change the function’s name 49

50 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-49 Choose Function Name screen Figure 14-48 Default INSERT statement for the tblMovies table 50

51 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-51 InsertRecordQuery function Figure 14-50 Wizard Results screen 51

52 Programming with Microsoft Visual Basic 2010, 5 th Edition The INSERT and DELETE Statements (cont’d.) Create a delete query Start the TableAdapter Query Configuration Wizard Click DELETE radio button On Choose a Query Type screen Default DELETE statement appears Change the function’s name 52

53 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-53 DeleteRecordQuery function Figure 14-52 SQL DELETE statement 53

54 Programming with Microsoft Visual Basic 2010, 5 th Edition The INSERT and DELETE Statements (cont’d.) Code Click event procedures for Add and Delete buttons Add button uses InsertRecordQuery function Delete button uses DeleteRecordQuery function Test the Add and Delete buttons 54

55 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-56 Selection structure entered in the btnDelete control’s Click event procedure Figure 14-55 Additional lines of code entered in the btnAdd_Click procedure 55

56 Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 14-57 Nested selection structure entered in the procedure Figure 14-58 Additional lines of code entered in the btnDelete_Click procedure 56

57 Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson C Summary Parameter query Created using a question mark in place of the criteria’s value in the WHERE clause The TableAdapter Query Configuration Wizard Can be used to save a query that contains the SELECT statement Can be used to associate a query containing the INSERT or DELETE statement with a function To invoke a query from code: Enter the query’s method or function in a procedure 57

58 Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson C Summary (cont’d.) The INSERT statement inserts records into a database The DELETE statement deletes records from a database 58


Download ppt "Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic 2010 5 th Edition."

Similar presentations


Ads by Google