CHAPTER EIGHT Accessing Data Processing Databases

advertisement
CHAPTER EIGHT
Accessing Data
Processing Databases
8- 2
Introduction
• Data is efficiently managed by storing them in
files on disk.
• Data files and programs are separate.
• The program does not need to be modified when
the data changes.
• Different programs can share the same data
sets.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
8- 3
Introduction (cont.)
• Most businesses organize their data into
databases.
• A database is an organizes collection of data.
• A database management system (DBMS) is a
program that is used to create and maintain
databases.
• Visual Basic .NET provides tools for developers
to access databases from their programs.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
8- 4
Objectives
• Understand and use relational databases.
• Understand a subset of the ADO.NET object
model.
• Apply the ADO.NET object model within Visual
Basic .NET to access databases.
• Use the properties and methods of the DataGrid
control to display record from a DataSet.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
8- 5
Objectives (cont.)
• Write SQL Select queries to extract data from a
database.
• Bind controls such as TextBoxes and Labels to a
DataSet.
• Create Master/Detail DataSets.
• Use several tools within Visual Basic .NET to
manage and use databases.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
8- 6
8.1 A Relational Database Primer
• Entities and Relationships
– Every database contains:
• Entities – anything of interest to a business.
• Relationships – real-world associations between
entities.
– Database developers often use an entityrelationship diagram (ERD).
• To document a database’s structure.
• Symbols are used to designate relationships
between entities.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
8- 7
8.1 A Relational Database Primer (cont.)
• Relational Database Tables
– A relational database stores the data for each
entity in a table with rows and columns.
– Compatible with Visual Basic .NET.
– A key field guarantees the uniqueness of records.
– A combination key is a key field with a
combination of two or more fields.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
8- 8
8.1 A Relational Database Primer (cont.)
– One-to-Many Relationships
• Established by placing the key field of the “one”
entity’s table into the “many” entity’s table as a
foreign key.
– Many-to-Many Relationships
• Constructed by creating a new table with the key
fields from related tables.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
8- 9
8.1 A Relational Database Primer (cont.)
– Normalized Databases
• Normalization is a process used to eliminate or
reduce data redundancy.
• Designers must determine which tables to place
information in.
• A table should be in third normal form.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
8- 10
8.1 A Relational Database Primer (cont.)
• Database Queries
– Most relational database systems use a query
language called structured query language
(SQL).
• To specify how to combine data in related tables.
• To specify how to select only the desired data.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
8- 11
8.2 An Introduction to ADO.NET
• Visual Basic .NET uses ADO.NET to manage
data in a database.
– ADO.NET (ActiveX Data Objects) uses an
extensive set of classes to manage data.
– This chapter covers the foundations of ADO.NET.
– Also we will cover some of the wizards and tools
provided to work with relational databases.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
8- 12
8.2 An Introduction to ADO.NET (cont.)
• An Overview of ADO.NET
– A DataProvider object directly interacts with a
database and its DBMS.
– A Connection object makes the physical
connection to the database.
– A DataSet class holds the actual data.
– User interface components work directly with the
DataSet.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
8- 13
8.3 Using ADO.NET with Visual Basic .NET
• Data Access Using the DataGrid Control
– Provides a method of displaying records from a
database in a grid-like manner.
– Building the DataAdapter
• Create a DataAdapter by a wizard.
– Building the DataSet
• Follow the procedure to create a DataSet.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
8- 14
8.3 Using ADO.NET with Visual Basic .NET
(cont.)
– Adding the DataGrid
• Follow the procedure to add a DataGrid to the
form.
• Set the DataGrid properties to connect it to the
DataAdapter and DataSet.
• Found on the Windows Form tab in the Toolbox.
• Will display fields specified in the SQL Select
statement.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
8.3 Using ADO.NET with Visual Basic .NET
(cont.)
• The DataGrid Control
– Designed to display the data stored in a DataSet.
– An expander allows navigation from a parent
table to a child table.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
8- 15
8.3 Using ADO.NET with Visual Basic .NET
(cont.)
– Properties
AllowSorting
AlternatingBackColor
BackColor
BackgroundColor
BorderStyle
CaptionBackColor
CaptionFont
CaptionForeColor
CaptionText
CaptionVisible
ColumnHeadersVisible
McGraw Hill/Irwin
CurrentCell
CurrentRowIndex
DataMember
DataSource
Enabled
FirstVisibleColumn
ForeColor
GridLineColor
GridLineStyle
HeaderBackColor
HeaderFont
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
8- 16
8.3 Using ADO.NET with Visual Basic .NET
(cont.)
– Properties (cont.)
HeaderForeColor
ParentRowsBackColor
ParentRowsForeColor
ParentRowsVisible
PreferredColumnWidth
ReadOnly
RowHeadersVisible
RowHeaderWidth
SelectionBackColor
SelectionForeColor
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
8- 17
8.3 Using ADO.NET with Visual Basic .NET
(cont.)
– Methods
•
•
•
•
•
•
Collapse(row)
Expand(row)
IsExpanded(row)
IsSelected(row)
Select(row)
SetDataBinding(dataSourceObject,
dataMemberString)
• UnSelect(row)
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
8- 18
8.3 Using ADO.NET with Visual Basic .NET
(cont.)
• Updating a Table
– ADO.NET provides support for table update.
– Changes in the DataGrid can be relayed back to
the original source data.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
8- 19
8.3 Using ADO.NET with Visual Basic .NET
(cont.)
• Parameterized Queries
– Users will often want to specify information that
will determine the results of a query.
– A parameter of a query could be a zipcode.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
8- 20
8.3 Using ADO.NET with Visual Basic .NET
(cont.)
• Master/Detail Record Display
– Here we examine a DataSet that includes two
tables related to a common field.
– A master/detail DataSet is a dataset with more
than one table.
– Each master record is related to zero or more
records from the second table.
– Parent and child terms may refer to master and
detail records.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
8- 21
8.4 Additional Visual Basic .NET Database
Tools
• We look at using the Server to work with
databases.
• We show how to add a data connection to the
server.
• The data connection will be used to create a
data adapter and data set.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
8- 22
8- 23
Chapter Summary
• A database is an organized collection of data
and relationships that describe entities of
interest to a business.
• An entity-relationship diagram (ERD)
documents entities and their relationships.
• Visual Basic .NET supports the relational
database approach for organizing data.
• Normalization is process used to minimize data
redundancy.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
8- 24
Chapter Summary (cont.)
• Visual Basic .NET uses ADO.NET to access and
manage databases.
• The DataGrid control is a control that can be
bound to a specific dataset.
• Parameterized queries provide for the ability to
determine how records will be selected at run
time.
• Many of Visual Basic .NET’s controls can be
bound to a dataset and its fields.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
8- 25
Chapter Summary (cont.)
• ADO.NET supports master/detail record
relationships.
• The Server Explorer provides access and
management tools for connections and servers.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
Download