600 likes | 616 Views
Learn how to connect a database to your VB.NET application, bind table and field objects to controls, and access records in a dataset.
E N D
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 11 Using ADO.NET 2.0
Objectives After studying this chapter, you should be able to: • Define the terms used when talking about databases • Connect a database to an application • Bind table and field objects to controls • Explain the purpose of the DataSet, BindingSource, TableAdapter, and BindingNavigator objects • Access the records in a dataset Microsoft Visual Basic 2005: Reloaded, Second Edition
Objectives (continued) • Write SQL SELECT statements • Create a query using the Query Configuration Wizard • Associate a ToolStrip control with a query Microsoft Visual Basic 2005: Reloaded, Second Edition
Database Terminology • Database: an organized collection of related information stored in a file on a disk • Relational database: a database that stores information in tables composed of columns and rows • Field: a single item of information • Record: a group of related fields • Table: a group of related records • Primary key: a field that uniquely identifies each record Microsoft Visual Basic 2005: Reloaded, Second Edition
Database Terminology (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Database Terminology (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Database Terminology (continued) • Parent table: contains a primary key • Child table: contains a foreign key from the parent table to link the tables • Foreign key: a 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 2005: Reloaded, Second Edition
ADO.NET 2.0 • ADO.NET 2.0: a technology to connect an application to a database • ADO: ActiveX Data Objects • Dataset: a copy of fields and records that the application wants to access • Connection to the database is temporary: • Only connected when retrieving data or making changes to data Microsoft Visual Basic 2005: Reloaded, Second Edition
ADO.NET 2.0 (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Connecting a Database to an Application Microsoft Visual Basic 2005: Reloaded, Second Edition
Connecting a Database to an Application (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Connecting a Database to an Application (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Previewing the Data Contained in a Dataset Microsoft Visual Basic 2005: Reloaded, Second Edition
Previewing the Data Contained in a Dataset (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Binding the Objects in a Dataset • Binding: connecting an object in a dataset to a control • 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 2005: Reloaded, Second Edition
Binding the Objects in a Dataset (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
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 columnar format • Use the list arrow next to an object’s name to change the type of control to be created Microsoft Visual Basic 2005: Reloaded, Second Edition
Having the Computer Create a Bound Control (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Having the Computer Create a Bound Control (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Having the Computer Create a Bound Control (continued) • DataGridView control: • AutoSizeColumnsMode property: select Fill to automatically adjust column widths to exactly fill the display area • BindingNavigator control: allows you to move from one record to the next in a dataset • Four objects are placed in the component tray: • DataSet • BindingSource • TableAdapter • BindingNavigator Microsoft Visual Basic 2005: Reloaded, Second Edition
Having the Computer Create a Bound Control (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Having the Computer Create a Bound Control (continued) • TableAdapter object: connects the database to the DataSet object • DataSet object: stores the information to be accessed from the database • BindingSource object: connects the DataSet object to the bound controls on the form Microsoft Visual Basic 2005: Reloaded, Second Edition
Having the Computer Create a Bound Control (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Having the Computer Create a Bound Control (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Having the Computer Create a Bound Control (continued) • Two event procedures are automatically created in Code Editor window: • bindingNavigatorSaveItem_Click • MainForm_Load • bindingNavigatorSaveItem_Click event: • Saves any changes made to the dataset • EndEdit method: applies pending changes • Update method: commits the changes to the database Microsoft Visual Basic 2005: Reloaded, Second Edition
Having the Computer Create a Bound Control (continued) • MainForm_Load event: • Fill method: TableAdapter object’s method to retrieve data from the database and store it in the dataset • DataGridView control: allows data to be edited directly within the control to update the database Microsoft Visual Basic 2005: Reloaded, Second Edition
Having the Computer Create a Bound Control (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Having the Computer Create a Bound Control (continued) • 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 Microsoft Visual Basic 2005: Reloaded, Second Edition
Having the Computer Create a Bound Control (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Binding to an Existing Control • Two ways to bind to an existing control: • 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, and TableAdapter objects are added to the component tray • BindingNavigator control is NOT added automatically Microsoft Visual Basic 2005: Reloaded, Second Edition
Binding to an Existing Control (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Binding to an Existing Control (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Binding to an Existing Control (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Accessing the Records in a Dataset • BindingSource properties: • Position: stores position of current record; position number is zero-relative • Move: moves the record pointer’s position Microsoft Visual Basic 2005: Reloaded, Second Edition
Accessing the Records in a Dataset (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Accessing the Records in a Dataset (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Accessing the Records in a Dataset (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Accessing the Records in a Dataset (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
DataSet Designer • DataSet Designer: • Indicates the order in which to display the data • Specifies fields and records to be viewed Microsoft Visual Basic 2005: Reloaded, Second Edition
DataSet Designer (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
DataSet Designer (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
DataSet Designer (continued) • Extensible Markup Language (XML): a text-based language used to store and share data between applications and across networks • XML schema definition file: • Defines the tables and fields that make up the dataset • Has extension of .xsd • Query: • Specifies the fields and records to retrieve from the database • Specifies the field order Microsoft Visual Basic 2005: Reloaded, Second Edition
DataSet Designer (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
DataSet Designer (continued) • Structured Query Language (SQL): a set of commands to access and manipulate database data • SELECT statement: • Used to specify the fields and records to retrieve, as well as the order of display • WHERE clause: limit the records to be selected • ORDER BY clause: control the order of display Microsoft Visual Basic 2005: Reloaded, Second Edition
DataSet Designer (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Creating a New Query Microsoft Visual Basic 2005: Reloaded, Second Edition
Creating a New Query (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Creating a New Query (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Creating a New Query (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Creating a New Query (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition