Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
-
Upload
neil-brown -
Category
Documents
-
view
225 -
download
3
Transcript of Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
![Page 1: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/1.jpg)
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Chapter TwelveAccess Databases and LINQ
![Page 2: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/2.jpg)
Objectives
After studying this chapter, you should be able to:
• Define the terms used when talking about databases
• Connect an application to a Microsoft Access database
• Bind table and field objects to controls
• Explain the purpose of the DataSet, BindingSource, TableAdapter, TableAdapterManager, and BindingNavigator objects
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 2
![Page 3: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/3.jpg)
Objectives (cont'd.)
• Customize a DataGridView control
• Handle errors using the Try…Catch statement
• Position the record pointer in a dataset
• Access the value stored in a field object
• Query a dataset using LINQ
• Customize a BindingNavigator control
• Use the LINQ aggregate methods
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 3
![Page 4: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/4.jpg)
Database Terminology
• Computer database: electronic file containing an organized collection of related information
• Relational database: database that stores information in tables composed of columns and rows
• Field: single item of information
• Record: group of related fields
• Table: group of related records
• Primary key: field that uniquely identifies each record
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 4
![Page 5: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/5.jpg)
Database Terminology (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 5
Figure 12-1: Example of a one-table relational database
![Page 6: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/6.jpg)
Database Terminology (cont'd.)
• Parent table: contains a primary key
• Child table: contains a foreign key from the parent table to link the tables
• Foreign key: field in a table that contains the primary key of another table
• Relational database advantages:– Less redundancy– Fast retrieval– Ability to selectively retrieve data
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 6
![Page 7: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/7.jpg)
Database Terminology (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 7
Figure 12-2: Example of a two-table relational database
![Page 8: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/8.jpg)
Connecting an Application to a Microsoft Access Database
• Microsoft Access database:– Has a file extension of .accdb
• Must connect an application to the database before the application can access the data
• Use the Data Source Configuration Wizard to connect to a database
• Dataset: copy of the fields and records stored in the computer’s internal memory, which the application can access
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 8
![Page 9: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/9.jpg)
Connecting an Application to a Microsoft Access Database (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 9
Figure 12-3: Data contained in the tblEmploy table
![Page 10: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/10.jpg)
Connecting an Application to a Microsoft Access Database (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 10
Figure 12-4: How to connect an application to an Access database
![Page 11: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/11.jpg)
Connecting an Application to a Microsoft Access Database (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 11
Figure 12-5: Result of running the Data Source Configuration Wizard
![Page 12: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/12.jpg)
Connecting an Application to a Microsoft Access Database (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 12
Figure 12-6: How to preview the contents of a dataset
![Page 13: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/13.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 13
Figure 12-7: EmployeesDataSet shown in the Preview Data dialog box
![Page 14: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/14.jpg)
Binding the Objects in a Dataset
• Binding: connecting an object in a dataset to a control on a form
• Bound controls: controls that are connected to an object in a dataset
• Can bind an object to:– An existing control in the interface– A control the computer creates for you
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 14
![Page 15: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/15.jpg)
Binding the Objects in a Dataset (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 15
Figure 12-8: How to bind an object in a dataset
![Page 16: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/16.jpg)
Having the Computer Create a Bound Control
• To allow the computer to create a bound control:– Drag the object from the dataset to the form
• DataGridView control: displays table data in a row and column format
• Use the list arrow next to an object’s name to change the type of control to be created
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 16
![Page 17: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/17.jpg)
Having the Computer Create a Bound Control (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 17
Figure 12-9: Icons in the Data Sources window
![Page 18: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/18.jpg)
Having the Computer Create a Bound Control (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 18
Figure 12-10: Result of clicking the tblEmploy table object’s list arrow
![Page 19: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/19.jpg)
Having the Computer Create a Bound Control (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 19
Figure 12-11: Result of clicking the Last_Name filed object’s list arrow
![Page 20: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/20.jpg)
Having the Computer Create a Bound Control (cont'd.)
• BindingNavigator control:– Allows movement to first, last, next, or previous
record– Allows direct selection of record by number– Allows you to add or delete a record– Allows you to save changes made to the dataset
• Five objects are placed in the component tray:– DataSet, BindingSource, TableAdapter, TableAdapterManager, BindingNavigator
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 20
![Page 21: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/21.jpg)
Having the Computer Create a Bound Control (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 21
Figure 12-12: Result of dragging the table object to the form
![Page 22: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/22.jpg)
Having the Computer Create a Bound Control (cont'd.)
• TableAdapter object: connects the database to the DataSet object
• DataSet object: stores the information to be accessed from the database
• TableAdapterManager object: handles saving data to multiple tables in the DataSet
• BindingSource object: connects the DataSet object to the bound controls on the form
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 22
![Page 23: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/23.jpg)
Having the Computer Create a Bound Control (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 23
Figure 12-13: Illustration of the relationships among the database, the objects in the component tray, and the bound controls
![Page 24: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/24.jpg)
The DataGridView Control
• DataGridView control: displays data in a row and column format– Each row represents a record– Each column represents a field
• Cell: the intersection of a row and column
• DataGridView has a task list used to control its appearance and behavior
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 24
![Page 25: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/25.jpg)
The DataGridView Control (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 25
Figure 12-14: Task list for a DataGridView control
![Page 26: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/26.jpg)
The DataGridView Control (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 26
Figure 12-15: Purpose of each task in the DataGridView’s task list
![Page 27: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/27.jpg)
The DataGridView Control (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 27
Figure 12-15: Edit Columns dialog box
![Page 28: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/28.jpg)
The DataGridView Control (cont'd.)
• Many properties of DataGridView are listed only in the Properties window
• AutoSizeColumnsMode property: – Select Fill setting to automatically adjust column
widths to exactly fill the display area– Select ColumnHeader setting to adjust column
widths based on the header text
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 28
![Page 29: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/29.jpg)
The DataGridView Control (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 29
Figure 12-17: DataGridView control after setting some of its properties
![Page 30: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/30.jpg)
Visual Basic Code
• DataGridView control allows data to be edited directly within the control to update the database
• Two event procedures are automatically created in Code Editor window when a table or field object is dragged to the form– MainForm_Load– bindingNavigatorSaveItem_Click
• MainForm_Load event:– Fill method: TableAdapter object’s method to
retrieve data from the database and store it in the dataset
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 30
![Page 31: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/31.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 31
Visual Basic Code (cont'd.)
• bindingNavigatorSaveItem_Click event: – Saves any changes made to the dataset– EndEdit method: applies pending changes to the
dataset– UpdateAll method: commits the dataset changes
to the database
• Be sure to use error-handling code
![Page 32: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/32.jpg)
Visual Basic Code (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 32
Figure 12-18: Code automatically entered in the Code Editor window
![Page 33: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/33.jpg)
Handling Errors in the Code
• Exception: an error that occurs while an application is running
• VB “handles” errors if the program does not by showing an error message and terminating the application
• Try…Catch statement: used to trap errors and attempt to handle them– Place code that could cause an exception within the
Try block– Catch block is executed if an exception occurs
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 33
![Page 34: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/34.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 34
Figure 12-19: How to use the Try…Catch statement
![Page 35: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/35.jpg)
Handling Errors in the Code (cont’d.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 35
Figure 12-19: How to use the Try…Catch statement (cont’d.)
![Page 36: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/36.jpg)
Handling Errors in the Code (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 36
Figure 12-20: Try…Catch statement entered in the Save Data button’s Click event procedure
![Page 37: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/37.jpg)
Handling Errors in the Code (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 37
Figure 12-21: Sample run of the Morgan Industries application
![Page 38: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/38.jpg)
Handling Errors in the Code (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 38
• BindingNavigator control provides buttons for first, last, previous, and next record and for adding records, deleting records, and saving changes • Can also use the control to access a record by its
record number
![Page 39: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/39.jpg)
The Copy to Output Directory Property
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 39
• Local database file: database file contained in a project
• Copy to Output Directory property: determines how VB saves changes to a local database file
• Copy always setting: database file is copied from the project folder to bin\Debug folder each time the application is started– Changes are made only to the bin\Debug copy
• Copy if newer setting: newer of the two database files (project folder and bin\Debug folder) is saved in bin\Debug folder
![Page 40: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/40.jpg)
The Copy to Output Directory Property (cont’d.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 40
Figure 12-22: How to use the Copy to Output Directory property
![Page 41: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/41.jpg)
Binding to an Existing Control
• Can bind an object in a dataset to an existing control in two ways:– Drag an object in the dataset to a control on the form
– Select the control and set properties
• Properties to bind the control are specific to the control
• DataSet, BindingSource,TableAdapter, and TableAdapterManager objects are added to the component tray
• BindingNavigator control is NOT added automatically
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 41
![Page 42: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/42.jpg)
Binding to an Existing Control (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 42
Figure 12-23: Result of dragging field objects to existing label controls
![Page 43: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/43.jpg)
Binding to an Existing Control (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 43
Figure 12-24: Sample run of a different version of the Morgan Industries application
![Page 44: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/44.jpg)
Coding the Next Record and Previous Record Buttons
• BindingSource object uses an invisible record pointer to track the current record in the dataset
• Position property: stores position of current record; this position number is zero-relative
• Move method: moves the record pointer’s position to first, last, previous, or next record in the dataset
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 44
![Page 45: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/45.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 45
Figure 12-23: How to use the BindingSource object’s Position property
![Page 46: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/46.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 46
Figure 12-26: How to use the BindingSource object’s Move methods
![Page 47: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/47.jpg)
Coding the Next Record and Previous Record Buttons (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 47
Figure 12-27: Code entered in the Click event procedures for the Next Record and Previous Record buttons
![Page 48: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/48.jpg)
Accessing the Value Stored in a Field
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 48
Figure 12-28: How to access the value stored in a field object
![Page 49: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/49.jpg)
Creating a Query
• Can arrange records in a dataset in any order
• Query: specifies the records to select in a dataset
• Language Integrated Query (LINQ): language used to create a query– Where clause: optional, specifies a condition to limit
which records to view– Order By clause: optional, specifies whether to
arrange in descending or ascending order
• Option Infer On: allows computer to infer the data type from the variables in the query
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 49
![Page 50: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/50.jpg)
Creating a Query (cont’d.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 50
Figure 12-29: How to use LINQ to select and arrange records in a dataset
![Page 51: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/51.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 51
Figure 12-29: How to use LINQ to select and arrange records in a dataset (cont’d.)
![Page 52: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/52.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 52
Figure 12-30: How to assign a LINQ variable’s contents to a BindingSource control
Creating a Query (cont’d.)
![Page 53: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/53.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 53
Figure 12-31: LINQ code entered in the Find Last Name button’s Click event procedure
![Page 54: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/54.jpg)
Creating a Query (cont’d.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 54
Figure 12-32: Employees whose last name begins with the letter S
![Page 55: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/55.jpg)
Personalizing a BindingNavigator Control
• You can include other items on the BindingNavigator control:– Buttons, text boxes, or drop-down buttons
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 55
![Page 56: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/56.jpg)
Personalizing a BindingNavigator Control (cont’d.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 56
Figure 12-33: How to manipulate the items on a BindingNavigator control
![Page 57: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/57.jpg)
Personalizing a BindingNavigator Control (cont’d.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 57
Figure 12-34: Items Collection Editor window
![Page 58: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/58.jpg)
Personalizing a BindingNavigator Control (cont’d.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 58
Figure 12-35: DropDownButton added to the BindingNavigator control
![Page 59: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/59.jpg)
Using the LINQ Aggregate Operators
• Aggregate operator: returns a single value from a group of values
• Most commonly used aggregate operators:– Average, Count, Max, Min, and Sum
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 59
![Page 60: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/60.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 60
Figure 12-36: How to use the LINQ aggregate operators
![Page 61: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/61.jpg)
Using the LINQ Aggregate Methods (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 61
Figure 12-37: Code associated with the three items on the DropDownButton
![Page 62: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/62.jpg)
Using the LINQ Aggregate Methods (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 62
Figure 12-37: Code associated with the three items on the DropDownButton (cont’d.)
![Page 63: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/63.jpg)
Using the LINQ Aggregate Methods (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 63
Figure 12-37: Code associated with the three items on the DropDownButton (cont’d.)
![Page 64: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/64.jpg)
Adding Items to the BindingNavigator Control (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 64
Figure 12-38: Average pay rate for part-time employees
![Page 65: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/65.jpg)
• Creating the Trivia Game application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 65
Programming Tutorial 1
Figure 12-40: MainForm for the Trivia Game application
![Page 66: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/66.jpg)
• Creating the CD Collection application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 66
Programming Tutorial 2
Figure 12-50: Preview Data dialog box showing the data stored in the CDDataSet
![Page 67: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/67.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 67
Programming Example
• Cartwright Industries application
Figure 12-55: MainForm in the Cartwright Industries application
![Page 68: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/68.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 68
Summary
• Can use Visual Basic to access data stored in databases
• Relational database: stores information in tables composed of fields and records
• Primary key: field in a database table that uniquely identifies each record
• Data in a relational database can be displayed in any order, and you can control the amount of information to view
![Page 69: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/69.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 69
Summary (cont'd.)
• You must connect the application to a database to create a dataset
• Display dataset information by binding controls to dataset objects
• TableAdapter: connects a database to a DataSet object
• BindingSource object: connects a DataSet object to bound controls on a form
• DataGridView control displays data in row and column format
![Page 70: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/70.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 70
Summary (cont'd.)
• Use a Try…Catch statement to handle exceptions
• Database file’s Copy to Output Directory property: determines when and if the file is copied to the project’s bin\Debug folder
• BindingSource object’s Position property: stores the location of the record pointer in a dataset
• BindingSource object’s Move methods: used to move the record pointer in a dataset
• Can access the value stored in a field object in a dataset
![Page 71: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.](https://reader035.fdocuments.net/reader035/viewer/2022062407/56649e8f5503460f94b92931/html5/thumbnails/71.jpg)
Summary (cont'd.)
• Use LINQ to select and arrange records in a dataset
• LINQ provides Average, Sum, Count, Min, and Max aggregate methods
• Can include additional items such as text boxes and drop-down buttons on a BindingNavigator control
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 71