Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.

Similar presentations


Presentation on theme: "Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL."— Presentation transcript:

1 Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL

2 Previewing the Academy Award Winners Application Programming with Microsoft Visual Basic 20122 Figure 14-1 Academy Award Winners application The 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

3 Previewing the Academy Award Winners Application (cont.) Programming with Microsoft Visual Basic 20123 Figure 14-2 Result of adding the missing record

4 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 Programming with Microsoft Visual Basic 20124

5 Add and delete records without using a BindingNavigator control Microsoft database is stored in the 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 Programming with Microsoft Visual Basic 20125 Adding Records to a Dataset Figure 14-3 Data contained in the tblMovies table

6 Programming with Microsoft Visual Basic 20126 Adding Records to a Dataset (cont.) Figure 14-4 Interface for the Academy Award Winners application

7 Programming with Microsoft Visual Basic 20127 Adding Records to a Dataset (cont.) Figure 14-6 Syntax and examples of adding a record to a dataset

8 Programming with Microsoft Visual Basic 20128 Adding Records to a Dataset (cont.) Figure 14-7 New record added to the DataGridView control TableAdapter object’s Update method –Used to save the changes to the database associated with the dataset Good practice: –Place the Update method within the Try block of a Try…Catch statement Because errors can occur when saving data

9 Programming with Microsoft Visual Basic 20129 Adding Records to a Dataset (cont.) Figure 14-8 Syntax and examples of saving dataset changes to a database

10 Programming with Microsoft Visual Basic 201210 Adding Records to a Dataset (cont.) Figure 14-10 Result of trying to add a duplicate record Figure 14-9 Additional code entered in the btnAdd_Click procedure

11 Programming with Microsoft Visual Basic 201211 Adding Records to a Dataset (cont.) Figure 14-11 Completed btnAdd_Click procedure

12 Programming with Microsoft Visual Basic 201212 Adding Records to a Dataset (cont.) Figure 14-12 New records displayed in the DataGridView control

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

14 Programming with Microsoft Visual Basic 201214 Sorting the Records in a Dataset (cont.) Figure 14-14 Records sorted by the YearWon field

15 Programming with Microsoft Visual Basic 201215 Deleting Records from a Dataset Figure 14-15 MessageBox.Show method entered in the btnDelete_Click procedure Delete button’s Click event procedure –Deletes the record whose YearWon field contains the value entered in the txtDeleteYear control Before deleting the record, display a message that asks the user for confirmation –Use the MessageBox.Show method

16 Programming with Microsoft Visual Basic 201216 Deleting Records from a Dataset (cont.) Figure 14-16 Syntax and examples of locating a record in a dataset

17 Programming with Microsoft Visual Basic 201217 Deleting Records from a Dataset (cont.) Figure 14-18 Additional code entered in the btnDelete_Click procedure Figure 14-17 Syntax and an example of deleting a record from a dataset Figure 14-19 Message box displayed by the btnDelete_Click procedure

18 Programming with Microsoft Visual Basic 201218 Deleting Records from a Dataset (cont.) Figure 14-20 frmMain_Load, btnAdd_Click, and btnDelete_Click procedures (continues)

19 Programming with Microsoft Visual Basic 201219 Deleting Records from a Dataset (cont.) Figure 14-20 frmMain_Load, btnAdd_Click, and btnDelete_Click procedures (continued)

20 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.FindByfieldName(value) Use the DataRow variable’s Delete method to delete a record from a dataset 20Programming with Microsoft Visual Basic 2012

21 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 Programming with Microsoft Visual Basic 201221

22 Programming with Microsoft Visual Basic 201222 Structured Query Language SQL (Structured Query Language) –A set of statements that allows you to perform common database tasks Examples: Storing, retrieving, updating, deleting, and sorting data –Can be used with a variety of database management systems and computers of all sizes

23 Programming with Microsoft Visual Basic 201223 The SELECT Statement 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 WHERE clause and ORDER BY clause –Optional parts of the syntax –WHERE is used to limit the records displayed –ORDER BY is used to sort records

24 Programming with Microsoft Visual Basic 201224 The SELECT Statement (cont.) Figure 14-23 Syntax and examples of the SELECT statement

25 Programming with Microsoft Visual Basic 201225 Creating a Query Figure 14-24 Contents of the dataset displayed in the DataGridView control Figure 14-25 DataSet Designer window

26 Programming with Microsoft Visual Basic 201226 Creating a Query (cont.) Figure 14-26 Choose a Command Type screen Figure 14-27 Choose a Query Type screen Point to Add on the shortcut menu and click Query Choose SQL statements as the command type Choose a query type Two ways to build a query: –Type the statement yourself –Use the Query Builder button Opens the Query Builder dialog box

27 Programming with Microsoft Visual Basic 201227 Creating a Query (cont.) Figure 14-28 Specify a SQL SELECT statement screen Figure 14-29 Query Builder dialog box

28 Programming with Microsoft Visual Basic 201228 Creating a Query (cont.) Figure 14-30 Records listed in the Results pane Figure 14-31 SELECT statement containing a WHERE clause

29 Programming with Microsoft Visual Basic 201229 Creating a Query (cont.) Figure 14-32 Result of executing the current query Figure 14-33 Records displayed in ascending order by the Title field

30 Programming with Microsoft Visual Basic 201230 Creating a Query (cont.) Figure 14-34 Records displayed by the current query

31 Programming with Microsoft Visual Basic 201231 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 Programming with Microsoft Visual Basic 201232 Lesson B Summary (cont.) To start the TableAdapter Query Configuration Wizard: –Right-click the table adapter’s name in the DataSet Designer window –Point to Add on the shortcut menu and click Query To open the Query Builder dialog box: –Use the TableAdapter Query Configuration Wizard –Specify a SQL SELECT statement screen, and then click the Query Builder button The % wildcard is used to represent characters in the WHERE clause’s condition

33 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 Programming with Microsoft Visual Basic 201233

34 Programming with Microsoft Visual Basic 201234 Parameter Queries Figure 14-35 Examples of parameter queries A parameter query asks users to supply input A parameter marker is a question mark (?) –Used in a parameter query –Allows the user to specify query parameters

35 Parameter Queries (cont.) Programming with Microsoft Visual Basic 201235 Figure 14-36 Records displayed in the DataGridView control Figure 14-37 Query Parameters dialog box

36 Parameter Queries (cont.) Programming with Microsoft Visual Basic 201236 Figure 14-38 Records with a YearWon field value of at least 2006

37 Saving a Query Programming with Microsoft Visual Basic 201237 For an application to use a query during run time, you must save the query and invoke it from code Use the TableAdapter Query Configuration Wizard –Save a query that contains the SELECT statement –Associate the query with one or more methods

38 Saving a Query (cont.) Programming with Microsoft Visual Basic 201238 Figure 14-40 Default query in the Specify a SQL SELECT statement screen Figure 14-39 Interface for the Academy Award Winners application in Lesson C

39 Saving a Query (cont.) Programming with Microsoft Visual Basic 201239 Figure 14-42 Completed Choose Methods to Generate screen Figure 14-41 Parameter query in the Specify a SQL SELECT statement screen

40 Saving a Query (cont.) Programming with Microsoft Visual Basic 201240 Figure 14-43 Wizard Results screen Figure 14-44 Method names included in the DataSet Designer window

41 Methods associated with a query c an be used to invoke the query during run time Next example: –Enter appropriate methods in the Display button’s Click event procedure Invoking a Query from Code Programming with Microsoft Visual Basic 201241 Figure 14-45 If clause and Fill method entered in the procedure

42 Invoking a Query from Code (cont.) Programming with Microsoft Visual Basic 201242 Figure 14-46 Additional code entered in the procedure Figure 14-47 btnDisplay_Click procedure Figure 14-48 2008 record shown in the interface

43 The INSERT and DELETE Statements Programming with Microsoft Visual Basic 201243 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 an Insert query and a Delete query

44 The INSERT and DELETE Statements (cont.) Programming with Microsoft Visual Basic 201244 Figure 14-50 Syntax and examples of the SQL DELETE statement Figure 14-49 Syntax and examples of the SQL INSERT statement

45 The INSERT and DELETE Statements (cont.) Programming with Microsoft Visual Basic 201245 Figure 14-51 Records displayed in the TblMoviesDataGridView control

46 The INSERT and DELETE Statements (cont.) Programming with Microsoft Visual Basic 201246 Figure 14-52 Choose a Query Type screen

47 The INSERT and DELETE Statements (cont.) Programming with Microsoft Visual Basic 201247 Figure 14-55 Wizard Results screen Figure 14-53 Default INSERT statement for the tblMovies table Figure 14-54 Choose Function Name screen Create an insert query –Start the TableAdapter Query Configuration Wizard –Verify that the Use SQL statements radio button is selected –Click the INSERT radio button on the Choose a Query Type screen The default INSERT statement appears –Change the function’s name

48 The INSERT and DELETE Statements (cont.) Programming with Microsoft Visual Basic 201248 Figure 14-57 SQL pane in the Query Builder dialog box Figure 14-56 InsertRecordQuery function Figure 14-58 SQL DELETE statement

49 The INSERT and DELETE Statements (cont.) Programming with Microsoft Visual Basic 201249 Figure 14-60 Selection structure entered in the btnAdd_Click procedure Figure 14-59 DeleteRecordQuery function Create a delete query –Start the TableAdapter Query Configuration Wizard –Click the DELETE radio button on the Choose a Query Type screen The default DELETE statement appears –Change the function’s name

50 The INSERT and DELETE Statements (cont.) Programming with Microsoft Visual Basic 201250 Figure 14-62 Additional code entered in the btnDelete_Click procedure Figure 14-63 Additional code entered in the selection structure Figure 14-61 Additional lines of code entered in the btnAdd_Click procedure

51 The INSERT and DELETE Statements (cont.) Programming with Microsoft Visual Basic 201251 Figure 14-64 Most of the application’s code

52 The INSERT and DELETE Statements (cont.) Programming with Microsoft Visual Basic 201252 Figure 14-65 Two records added to the database

53 Programming with Microsoft Visual Basic 201253 Lesson C Summary Parameter query –Created using a question mark in place of the criteria’s value in the WHERE clause 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

54 Programming with Microsoft Visual Basic 201254 Lesson C Summary (cont.) The INSERT statement inserts records into a database The DELETE statement deletes records from a database


Download ppt "Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL."

Similar presentations


Ads by Google