Chapter 12

advertisement
Chapter 12: ADO.NET and
ASP.NET
Programming with Microsoft Visual Basic
.NET, Second Edition
Database Terminology
Lesson A Objectives
• Define the terms used when talking about
databases
• Explain the purpose of the DataAdapter,
Connection, and DataSet objects
• Explain the role of the provider
• Create and configure an OleDbDataAdapter
object
Programming with Microsoft Visual Basic .NET, Second Edition
2
Database Terminology
Lesson A Objectives (continued)
• Write SQL SELECT statements
• Create a dataset
• Display a dataset in a DataGrid control
Programming with Microsoft Visual Basic .NET, Second Edition
3
Database Terminology (continued)
• Database: organized collection of related
information stored in a file on a disk
• Relational database: database that stores
information in tables
– Each column represents a field
– Each row represents a record
Programming with Microsoft Visual Basic .NET, Second Edition
4
Database Terminology (continued)
• Table: a group of related records
• Each record in a table pertains to the same topic,
and each contains the same type of information
• Relational database contains one or more tables
• Primary key: a field that uniquely identifies each
record in a table
Programming with Microsoft Visual Basic .NET, Second Edition
5
Database Terminology (continued)
Figure 12-3: Example of a two-table relational database
Programming with Microsoft Visual Basic .NET, Second Edition
6
Database Terminology (continued)
• In Figure 12-3:
– The first table is called the parent table
– The second table is called the child table
– In the parent table, the Number field is the primary
key
– In the child table, the Number field is called the
foreign key
Programming with Microsoft Visual Basic .NET, Second Edition
7
ADO.NET
• To connect an application to a database, Visual
Basic .NET uses ADO.NET
• With ADO.NET, the connection between an
application and a database is a temporary one
• Use three ADO.NET objects and a provider to
access a database from a Visual Basic .NET
application
– DataAdapter, Connection, and DataSet
Programming with Microsoft Visual Basic .NET, Second Edition
8
ADO.NET (continued)
Figure 12-4: Illustration of the relationships among an
application, the ADO.NET objects, a provider, and a database
Programming with Microsoft Visual Basic .NET, Second Edition
9
Creating and Configuring a
DataAdapter Object
• The DataAdapter object is the link between the
application and the Connection object
• DataAdapter object contacts the Connection
object whenever the application needs to read
data from or write data to a database
Programming with Microsoft Visual Basic .NET, Second Edition
10
Creating and Configuring a
DataAdapter Object (continued)
Figure 12-6: Procedure for creating and configuring a
DataAdapter object for a Microsoft Access database
Programming with Microsoft Visual Basic .NET, Second Edition
11
Creating and Configuring a
DataAdapter Object (continued)
Figure 12-6: Procedure for creating and configuring a
DataAdapter object for a Microsoft Access database (continued)
Programming with Microsoft Visual Basic .NET, Second Edition
12
SQL
• SQL (Structured Query Language): a set of
commands to access and manipulate the data
stored in many database management systems
• SQL commands perform database tasks such as
storing, retrieving, updating, deleting, and sorting
Programming with Microsoft Visual Basic .NET, Second Edition
13
SQL (continued)
• SELECT statement allows you to:
– Specify the fields and records you want to view
– Control the order in which the fields and records
appear when displayed
Programming with Microsoft Visual Basic .NET, Second Edition
14
SQL (continued)
Figure 12-13: Syntax and examples of the SELECT statement
Programming with Microsoft Visual Basic .NET, Second Edition
15
SQL (continued)
Figure 12-13: Syntax and examples of the SELECT statement
(continued)
Programming with Microsoft Visual Basic .NET, Second Edition
16
Using the Query Builder to Enter a
SELECT Statement
• When you click the Next > button on the Choose
a Query Type screen, the Generate the SQL
statements screen appears
• You can enter the SELECT statement yourself, or
you can have the Query Builder enter it for you
• To use the Query Builder, click the Query Builder
button
– The Query Builder and Add Table dialog boxes
open
Programming with Microsoft Visual Basic .NET, Second Edition
17
Using the Query Builder to Enter a
SELECT Statement (continued)
Figure 12-15: Query Builder and Add Table dialog boxes
Programming with Microsoft Visual Basic .NET, Second Edition
18
Using the Query Builder to Enter a
SELECT Statement (continued)
Figure 12-18: SELECT statement entered in the Generate the
SQL statements screen
Programming with Microsoft Visual Basic .NET, Second Edition
19
Creating a Dataset
• A dataset contains the data you want to access
from the database, as specified in the SELECT
statement associated with the DataAdapter
object
• XML (Extensible Markup Language): a textbased language used to store and share data
between applications and across networks and
the Internet
• An XML schema definition file defines the tables
and fields that make up the dataset
Programming with Microsoft Visual Basic .NET, Second Edition
20
Creating a Dataset (continued)
Figure 12-21: Procedure for creating a dataset
Programming with Microsoft Visual Basic .NET, Second Edition
21
Using the Fill Method
• Use the DataAdapter object’s Fill method to fill a
dataset with data while an application is running
Figure 12-25: Syntax and an example of the Fill method
Programming with Microsoft Visual Basic .NET, Second Edition
22
Binding the DataSet Object to a
DataGrid Control
• View the data contained in a dataset by
connecting its DataSet object to one or more
controls in the interface
• Binding: Connecting a DataSet object to a control
• Bound controls: the connected controls
• Bind a control using one or more properties listed
in the Properties window
Programming with Microsoft Visual Basic .NET, Second Edition
23
Binding the DataSet Object to a
DataGrid Control (continued)
Figure 12-26: Procedure for binding a DataSet object to a
DataGrid control
Programming with Microsoft Visual Basic .NET, Second Edition
24
Binding the DataSet Object to a
DataGrid Control (continued)
• When bound to a DataSet object, the DataGrid
control displays the data from the dataset in a
row and column format
– Each field in the dataset appears in a column in
the DataGrid control
– Each record in the dataset appears in a row in the
DataGrid control
Programming with Microsoft Visual Basic .NET, Second Edition
25
Reconfiguring the DataAdapter
Object
Figure 12-30: Procedure
for reconfiguring an
existing DataAdapter
object
Programming with Microsoft Visual Basic .NET, Second Edition
26
More on Binding Controls
Lesson B Objectives
• Display a dataset in various controls in an
interface
• Position the record pointer in a dataset
Programming with Microsoft Visual Basic .NET, Second Edition
27
Binding the DataSet Object to a
Label Control or a Text Box
Figure 12-33: Procedure for binding a DataSet object to a label
control or text box
Programming with Microsoft Visual Basic .NET, Second Edition
28
Coding the Cartwright Industries
Application
• Carl Simons, the sales manager at Cartwright
Industries, records the item number, name, and
price of each product the company sells in a
database named Items.mdb
• Items.mdb database
– Contains one table named tblItems
– The Number and Name fields contain text, and the
Price field contains numbers
Programming with Microsoft Visual Basic .NET, Second Edition
29
Coding the Cartwright Industries
Application (continued)
Figure 12-37: Items.mdb database opened in Microsoft Access
Programming with Microsoft Visual Basic .NET, Second Edition
30
Coding the Cartwright Industries
Application (continued)
Figure 12-39: TOE chart for the Cartwright Industries application
Programming with Microsoft Visual Basic .NET, Second Edition
31
Creating Web Applications Using
ASP.NET
Lesson C Objectives
• Define the terms used when talking about the
Web
• Create a Web application
• Add controls to a Web form
• Start a Web application
Programming with Microsoft Visual Basic .NET, Second Edition
32
Creating Web Applications Using
ASP.NET
Lesson C Objectives (continued)
• Use the validator controls
• Include a list box on a Web form
• Determine whether a postback has occurred
• Include a DataGrid control on a Web form
Programming with Microsoft Visual Basic .NET, Second Edition
33
Web Terminology
• The Internet is the world’s largest computer
network, connecting millions of computers
located all around the world
• World Wide Web (WWW or the Web)
– Part of the Internet
– Consists of documents called Web pages that are
stored on Web servers
Programming with Microsoft Visual Basic .NET, Second Edition
34
Web Terminology (continued)
• Web server: computer that contains software that
“serves up” Web pages in response to requests
from clients
• A client is a computer that requests information
from a Web server
• The information is requested and subsequently
viewed through the use of a program called a
Web browser (or simply, a browser)
Programming with Microsoft Visual Basic .NET, Second Edition
35
Web Terminology (continued)
Figure 12-49: Illustration of the relationship between a client, a
browser, and a Web server
Programming with Microsoft Visual Basic .NET, Second Edition
36
Web Terminology (continued)
• Static Web page: document whose purpose is
merely to display information to the viewer
• Dynamic Web page is interactive; it can accept
information from the user and also retrieve
information for the user
• Every Web page has a unique address called a
URL (Uniform Resource Locator) that indicates
its location on the Web
Programming with Microsoft Visual Basic .NET, Second Edition
37
Creating Web Applications
• Use a Web form to create a Web page in Visual
Basic .NET
• Create (or design) the Web page in the Web
Form Designer window
Programming with Microsoft Visual Basic .NET, Second Edition
38
Creating Web Applications
(continued)
Figure 12-52: Procedure for creating a Web application
Programming with Microsoft Visual Basic .NET, Second Edition
39
Adding Controls to a Web Form
• Use the tools contained in the Toolbox window to
add controls to a Web form
• The tools for a Web form are located on the Web
Forms tab in the toolbox
• Can add a control to a Web form by simply
dragging the corresponding tool from the Web
Forms tab to the form
Programming with Microsoft Visual Basic .NET, Second Edition
40
Using the Web Validator Tools
• These tools allow you to validate user input
Figure 12-65: The
Web validator tools
Programming with Microsoft Visual Basic .NET, Second Edition
41
Including a List Box on a Web Form
• Use the ListBox tool on the Web Forms tab to
add a list box to a Web form
• Can use a list box on a Web form to display a list
of choices from which the user can select one or
more choices
Programming with Microsoft Visual Basic .NET, Second Edition
42
Including a List Box on a Web Form
(continued)
• A postback refers to the client requesting data
from the server, and the server responding
• Each time a postback occurs, the Web page is
redisplayed on the client’s screen
• Can use the Web form’s IsPostBack property to
determine if the Web form is being displayed for
the first time or as a result of a postback
Programming with Microsoft Visual Basic .NET, Second Edition
43
Including a DataGrid Control on a
Web Form
• DataGrid control displays the data from a dataset
in a row and column format
• Each field in the dataset appears in a column in
the DataGrid control, and each record appears in
a row
• The DataGrid control’s DataBind method is used
to bind the control to the dataset
Programming with Microsoft Visual Basic .NET, Second Edition
44
Including a DataGrid Control on a
Web Form (continued)
Figure 12-72: Web form for the Fairview application
Programming with Microsoft Visual Basic .NET, Second Edition
45
Including a DataGrid Control on a
Web Form (continued)
• DataGrid control provides many properties to
control the appearance of its output
• DataGrid control provides an Auto Format dialog
box that allows selection from a list of predefined
formats, or schemes, for displaying data
Programming with Microsoft Visual Basic .NET, Second Edition
46
Summary
• To access a database from a Visual Basic .NET
application, use the DataAdapter, Connection,
and DataSet objects
• Use SQL SELECT to specify the fields and
records to view, and to control the order in which
the fields and records appear when displayed
• Use the Fill method to fill a dataset with data
while an application is running
Programming with Microsoft Visual Basic .NET, Second Edition
47
Summary (continued)
• To bind a DataSet object to a DataGrid control
– Set DataGrid control’s DataSource property to the
name of the DataSet object
– Set DataMember property to the name of a table
• To bind a DataSet object to a list box
– Set the list box’s DataSource property to the name
of the DataSet object
– Set its DisplayMember property to the name of the
table and field
Programming with Microsoft Visual Basic .NET, Second Edition
48
Summary (continued)
• To add a control to a Web form, use the tools on
the Web Forms tab in the toolbox
• To validate user input on a Web page, use one or
more Web validator tools
• To determine whether a postback has occurred,
use the Web form’s IsPostBack property
• To bind a Web DataGrid control to a dataset, use
the DataBind method
Programming with Microsoft Visual Basic .NET, Second Edition
49
Download