Chapter 10

32
10-1 lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh

Transcript of Chapter 10

Page 1: Chapter 10

10-1

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

Page 2: Chapter 10

Accessing Database Files

Chapter1010

McGraw-Hill © 2006 The McGraw-Hill Companies, Inc. All rights reserved.

Page 3: Chapter 10

10-3

Objectives

Use database terminology correctlyCreate Windows and Web projects that display

database data Display data in a DataGridView controlBind data to text boxes and labelsAllow the user to select from a combo box or list box

and display the corresponding record in data-bound controls

Page 4: Chapter 10

10-4

Visual Basic and Database Files

VB projects can display and update the data from database files

VB .NET uses ADO.NET for database accessADO.NET is the next generation of database

technology, based on Microsoft's previous version, ActiveX Data Objects (ADO)

An advantage of ADO.Net is that information is stored and transferred in Extensivle Markup Language (XML)

Page 5: Chapter 10

10-5

Database Terminology

DatabasesTables – can be viewed like a spreadsheet

Records (rows) – data for one item, person, transaction

Fields (columns) – stores a different element of dataKey field (unique to each record)

Relational databaseMultiple tablesRelationships between the tables

Page 6: Chapter 10

10-6

Database Table Example

Page 7: Chapter 10

10-7

XML Data (1 of 2)

Industry-standard format for storing and transferring data

The XML needed for accessing databases will be automatically generated in Visual Basic

Data stored in XML is text, identified by tags-similar to HTML tags and can be edited by any text editor program

Tags in XML are not predefined as they are in HTMLTags can identify fields by name

Page 8: Chapter 10

10-8

XML Data (2 of 2)

In addition to an XML data file there is normally an XML schema fileSchema describes the fields, data types, and any

constraints, such as required fieldsADO.NET validates the data against the schema and

checks for constraint violationsSchema is defined with XML tags and can be

viewed/edited in a text editorADO.NET can treat the XML data as objects-allows

the IntelliSense feature of the VS.NET environment to provide information to the programmerData executes faster

Page 9: Chapter 10

10-9

Using ADO.NET and Visual Basic

Can display data from a database onWindows FormWeb Form

Add controls to form and bind data to themLabel, TextBoxSpecial controls designed just for data such as

DataGrid, DataList

Page 10: Chapter 10

10-10

The DataGrid View Control

The DataGridView control is bound to a table in a dataset. The data fields display automatically in the cells of the grid.

Page 11: Chapter 10

10-11

Data Access in Visual Studio 2005

Allows an easy way to create data-bound controls on a form

Can drag tables and fields from the window onto a form to automatically create controls that are bound to the data

Data can be displayed in grids or individual fields-referred to as details

Can drag a field from the Data Sources window and drop it on an existing control-causes data binding to be set up automatically

An .xsd file and the schema is added to the Server Explorer window; Binding Source, Table Adapter, and Data Set objects are added to the form’s component tray

Page 12: Chapter 10

10-12

Overview of Database Objects

Binding SourceEstablishes a link to the actual data; a specific file

and/or serverTable Adapter

Handles retrieving and updating the dataGenerates SQL statement that are used to

access or update dataDataset

Contains actual data, may come from multiple connections and/or multiple data adapters

Page 13: Chapter 10

10-13

Steps to Display Data in Bound Controls on a Form

Page 14: Chapter 10

10-14

Binding Sources

Object establishes a link from a specific file or database to the program

Can you a wizard to automatically create Binding Source objects –OR—

Add new Binding Source objects using the Data Sources window or the Data menu

Page 15: Chapter 10

10-15

Table Adapters

Does all the work of passing data back and forth between a data source (the binding source) and a program (the dataset)

Data does not have to be from a databaseData can be text file, object, or an arrayTransfers data to and from via XML

Page 16: Chapter 10

10-16

Datasets

Temporary set of data stored in memoryIn ADO.NET datasets are disconnected; the

copy of data kept in memory does not keep an active connection to the data source

Dataset may contain multiple tables and relationships

Any controls bound to the dataset will automatically fill with data

Page 17: Chapter 10

10-17

The Smart Tag

Smart Tags are used for many functions including:Selecting a data sourceEditing properties of grid columnsAutoFormat the gridPreview data

Preview helps users do a better job of designing output

Selecting AutoFormat allows the user to choose from many predefined format

Page 18: Chapter 10

10-18

Grid with Pre-Defined AutoFormat

Predefined formats can be applied from the AutoFormat item on the smart tag.

Page 19: Chapter 10

10-19

The Database Schema File

When a new data source is added to a project, a file .xsd is added to the Solution Explorer-the XML schema definition

XML file contains the description and properties of the data

The schema shows the names of the table(s) and fields, the primary keys for each table and the relationships among the tables

At the bottom of the schema appears the TableAdapter for the table which handles different methods for the table

Page 20: Chapter 10

10-20

.XSD File

The .xsd file holds the schema of the database, where table elements, relationships, and keys can be viewed and modified

Page 21: Chapter 10

10-21

Binding Individual Data Fields

Table fields from the dataset can be bound to many types of controls such as labels, text boxes, combo boxes, and check boxes

Controls that are connected to fields are bound controls or data-bound controls

Easiest way to create bound controls is to use the automatic binding feature of the Data Source windowCreates individual text box controls for each field of

data and navigation control allowing the user to more from one record to another

Page 22: Chapter 10

10-22

Each text box or check box is bound to one field from the table. As the user clicks thenavigation buttons, all controls change to display the data forthe next record

Data-Bound Text Boxes -Example

Page 23: Chapter 10

10-23

The Data Sources Window

This window is used to add a new data source by using the Data Source Configuration Wizard

Click the table name to make a drop-down list available to view Details

The table’s icon changes to match the view

Page 24: Chapter 10

10-24

Selecting Records from a List

Allows user to select records to display from a listFill a ListBox or ComboBox with values from the

database –OR—Display the information in a drop-down list and

allow the user to make a selection; the corresponding data elements fill remaining fields

Page 25: Chapter 10

10-25

Selecting Fields from the Table

Select individual fields when creating the new data source –OR– after creating the data source

When creating the data source choose the AddNewData Source option and follow the Configuration Wizard; expand the Tables node and palce a check mark on the desired fields

To modify after creating, select the dataset and use the Configuration Wizard and make the field selections from the Wizard

Page 26: Chapter 10

10-26

Data Source Configuration Wizard

Page 27: Chapter 10

10-27

Sorting the List Data

Sort records in a query that selects data for the dataset

SQL SELECT statement is generated automatically by the designer; can be modified

Use the Solutions Explorer and the schema file (.xsd extension) and the Property button (…)

Choose “Ascending” in the Sort Type cell of the Query Builder

Execute the query to preview the output

Page 28: Chapter 10

10-28

Sorting Data

Click the Fill,GetData() entry in the dataset schema to display the properties of the Fill command in the Properties window.

Select Ascending for the Sort Type to sort the data by the Title field. The SQL statement changes to include an ORDER BY clause

Page 29: Chapter 10

10-29

Choosing the Control Type for Fields

Dragging a Details view to a form displays text boxes for the default text fields

Click a field name and choose a control type in the Data Sources window

Page 30: Chapter 10

10-30

Selecting Records Using Web Forms

Web controls are much different from Windows controlsAdditional security issues for Web-based database

applicationsWeb database applications operate differently than

Windows applications due to web pages in a client/server environment

Web pages are stateless—each time a page displays it is a new page

Each time the user makes a selection from a list a postback occurs and the Web page redisplays with only the selected data

Page 31: Chapter 10

10-31

Security in Web Database Applications

Security is much tighter for Web database applications than for Windows applications

Require user authentication if setting up a Web application that displays or allows modification to data

Visual Studio 2005 integrates security features which are general strict by default

Page 32: Chapter 10

10-32

Moving Database Projects

Improvement from previous versions of VB.NETProject needs to be closed in the IDECopy the complete folder from one computer or

location to another

For Web projects, open the IDE, select Open Web Site and browse to the folder