+ All Categories
Home > Technology > MELJUN CORTES Visual basic 2005 08 database sql and ado .net

MELJUN CORTES Visual basic 2005 08 database sql and ado .net

Date post: 15-Jul-2015
Category:
Upload: meljun-cortes
View: 64 times
Download: 3 times
Share this document with a friend
Popular Tags:
30
Visual Basic 200 Visual Basic 200 5 5 VIII. DATABASE, SQL AND ADO .NET MELJUN CORTES MELJUN CORTES
Transcript

Visual Basic 200Visual Basic 20055VIII. DATABASE, SQL AND ADO .NET

MELJUN CORTESMELJUN CORTES

Database, SQL and ADO .NETDatabase, SQL and ADO .NETObjectives

2DATABASE, SQL AND ADO .NET

In this chapter you will learn:•The relational database model.•To write basic database queries in SQL.•To add data sources to projects.•To use the IDE's drag-and-drop capabilities to display database tables in applications.•To use the classes of namespaces System.Data and System.Data.SqlClient to manipulate databases.•To use ADO.NET's disconnected object model to store data from a database in local memory.•To create XML documents from data sources.

Visual Basic 2005

OverviewOverview

A database is an organized collection of data

Many strategies exist for organizing data to facilitate easy access and manipulation

A database management system(DBMS) provides mechanisms for storing, organizing, retrieving and modifying data for many users

3DATABASE, SQL AND ADO .NET

Visual Basic 2005

Database, SQL and ADO .NET

OverviewOverview

Today's most popular database systems are relational databases

A language called SQL pronounced “sequel,“ is the international standard language used almost universally with relational databases to perform queries and to manipulate data

Some popular relational database management systems (RDBMS) are Microsoft SQL Server, Oracle, Sybase, IBM DB2, MySQL and PostgreSQL

4DATABASE, SQL AND ADO .NET

Visual Basic 2005

Database, SQL and ADO .NET

Relational DatabasesRelational Databases

A relational database is a logical representation of data that allows the data to be accessed independently of its physical structure

A relational database organizes data in tables

5DATABASE, SQL AND ADO .NET

Visual Basic 2005

Database, SQL and ADO .NET

SQLSQL

Common SQL keywords

6DATABASE, SQL AND ADO .NET

Visual Basic 2005

Database, SQL and ADO .NET

SQL Keyword Description

SELECT Retrieves data from one or more tables.

FROM Specifies the tables involved in a query. Required in every query.

WHERE Specifies optional criteria for selection that determine the rows to be retrieved, deleted or updated.

ORDER BY Specifies optional criteria for ordering rows (e.g., ascending, descending).

INNER JOIN Specifies optional operator for merging rows from multiple tables.

INSERT Inserts rows in a specified table.

UPDATE Updates rows in a specified table.

DELETE Deletes rows from a specified table.

SQLSQL

A SQL query "selects" rows and columns from one or more tables in a database

Such selections are performed by queries with the SELECT keyword

The basic form of a SELECT query is:

7DATABASE, SQL AND ADO .NET

Visual Basic 2005

Database, SQL and ADO .NET

SELECT * FROM tableNameSELECT * FROM tableName

The asterisk (* ) indicates that all the columns from

the tableName table should be retrieved

SELECT column1, column2,… FROM tableNameSELECT column1, column2,… FROM tableName

Column list separated by comma (, )

SQLSQL

When users search a database for rows that satisfy certain selection criteria (formally called predicates), only rows that satisfy the selection criteria are selected

8DATABASE, SQL AND ADO .NET

Visual Basic 2005

Database, SQL and ADO .NET

SELECT column1, column2,… FROM tableName WHERE criteriaSELECT column1, column2,… FROM tableName WHERE criteria

SELECT Title, EditionNumber, Copyright FROM Titles WHERE Copyright > '2004'

SELECT Title, EditionNumber, Copyright FROM Titles WHERE Copyright > '2004'

Retrieve rows where Copyright is greater than

‘2004’

SQLSQL

The rows in the result of a query can be sorted into ascending or descending order by using the optional ORDER BY clause

9DATABASE, SQL AND ADO .NET

Visual Basic 2005

Database, SQL and ADO .NET

SELECT column1, column2,… FROM tableName ORDER BY columnList ASCOrSELECT column1, column2,… FROM tableName ORDER BY columnList DESC

SELECT column1, column2,… FROM tableName ORDER BY columnList ASCOrSELECT column1, column2,… FROM tableName ORDER BY columnList DESC

SELECT AuthorID, FirstName, LastNameFROM AuthorsORDER BY LastName

SELECT AuthorID, FirstName, LastNameFROM AuthorsORDER BY LastName

Optional

SQLSQL

Database designers typically normalize databases i.e., split related data into separate tables to ensure that a database does not store redundant data

Often, it is desirable to merge data from multiple tables into a single result

This is referred to as joining the tables, and is specified by an INNER JOIN operator in the query

10DATABASE, SQL AND ADO .NET

Visual Basic 2005

Database, SQL and ADO .NET

SQLSQL

An INNER JOIN merges rows from two tables by testing for matching values in a column that is common to the tables

The basic form of an INNER JOIN is:

11DATABASE, SQL AND ADO .NET

Visual Basic 2005

Database, SQL and ADO .NET

SELECT columnName1, columnName2, ... FROM table1 INNER JOIN table2     ON table1.columnName = table2.columnName

SELECT columnName1, columnName2, ... FROM table1 INNER JOIN table2     ON table1.columnName = table2.columnName

SELECT FirstName, LastName, ISBN FROM Authors INNER JOIN AuthorISBN      ON Authors.AuthorID = AuthorISBN.AuthorID ORDER BY LastName, FirstName

SELECT FirstName, LastName, ISBN FROM Authors INNER JOIN AuthorISBN      ON Authors.AuthorID = AuthorISBN.AuthorID ORDER BY LastName, FirstName

The ON clause of the INNER JOIN specifies the columns from each table that are compared to determine which

rows are merged

SQLSQL

The INSERT statement inserts a row into a table

The basic form of this statement is:

12DATABASE, SQL AND ADO .NET

Visual Basic 2005

Database, SQL and ADO .NET

INSERT INTO tableName ( columnName1, columnName2, ..., columnNameN ) VALUES ( value1, value2, ..., valueN )INSERT INTO tableName ( columnName1, columnName2, ..., columnNameN ) VALUES ( value1, value2, ..., valueN )

INSERT INTO Authors ( FirstName, LastName ) VALUES ( 'Sue', 'Smith' ) INSERT INTO Authors ( FirstName, LastName ) VALUES ( 'Sue', 'Smith' ) 

SQLSQL

An UPDATE statement modifies data in a table

The basic form of the UPDATE statement is:

13DATABASE, SQL AND ADO .NET

Visual Basic 2005

Database, SQL and ADO .NET

UPDATE tableName SET columnName1 = value1,     columnName2 = value2, ...,     columnNameN = valueN WHERE criteria

UPDATE tableName SET columnName1 = value1,     columnName2 = value2, ...,     columnNameN = valueN WHERE criteria

UPDATE Authors SET LastName = 'Jones' WHERE LastName = 'Smith' AND FirstName = 'Sue'

UPDATE Authors SET LastName = 'Jones' WHERE LastName = 'Smith' AND FirstName = 'Sue'

SQLSQL

A DELETE statement removes rows from a table

The basic form of a DELETE statement is:

14DATABASE, SQL AND ADO .NET

Visual Basic 2005

Database, SQL and ADO .NET

DELETE FROM tableName WHERE criteria DELETE FROM tableName WHERE criteria 

DELETE FROM Authors WHERE LastName = 'Jones' AND FirstName = 'Sue' DELETE FROM Authors WHERE LastName = 'Jones' AND FirstName = 'Sue' 

ADO .NET Object ModelADO .NET Object Model

The ADO.NET object model provides an API for accessing database systems programmatically

ADO.NET was created for the .NET framework to replace Microsoft's ActiveX Data Objects™ (ADO) technology

15DATABASE, SQL AND ADO .NET

Visual Basic 2005

Database, SQL and ADO .NET

ADO .NET Object ModelADO .NET Object Model

ADO .NET Architecture

16DATABASE, SQL AND ADO .NET

Visual Basic 2005

Database, SQL and ADO .NET

ADO .NET Object ModelADO .NET Object Model

Namespace System.Data is the root namespace for the ADO.NET API

The other important ADO.NET namespaces, System.Data.OleDb and System.Data.SqlClient, contain classes that enable programs to connect with and manipulate data sources locations that contain data

17DATABASE, SQL AND ADO .NET

Visual Basic 2005

Database, SQL and ADO .NET

ADO .NET Object ModelADO .NET Object Model

Namespace System.Data.OleDb contains classes that are designed to work with any data source

Namespace System.Data.SqlClient contains classes that are optimized to work with Microsoft SQL Server databases

An object of class SqlConnection (namespace System.Data.SqlClient) represents a connection to a data source specifically a SQL Server database

18DATABASE, SQL AND ADO .NET

Visual Basic 2005

Database, SQL and ADO .NET

ADO .NET Object ModelADO .NET Object Model

An object of class SqlCommand (namespace System.Data.SqlClient) represents a SQL command that a DBMS can execute on a database

A program can use SqlCommand objects to manipulate a data source through a SqlConnection

A connection that remains active for some length of time to permit multiple data operations is known as a persistent connection

19DATABASE, SQL AND ADO .NET

Visual Basic 2005

Database, SQL and ADO .NET

ADO .NET Object ModelADO .NET Object Model

Class DataTable (namespace System.Data) represents a table of data

A DataTable contains a collection of DataRows that represent the table's data

A DataTable also has a collection of DataColumns that describe the columns in a table

DataRow and DataColumn are both located in namespace System.Data

20DATABASE, SQL AND ADO .NET

Visual Basic 2005

Database, SQL and ADO .NET

ADO .NET Object ModelADO .NET Object Model

An object of class System.Data.DataSet, which consists of a set of DataTables and the relationships among them, represents a cache of data that a program stores temporarily in local memory

The structure of a DataSet mimics the structure of a relational database

DATABASE, SQL AND ADO .NET 21

Visual Basic 2005

ADO .NET Object ModelADO .NET Object Model

An advantage of using class DataSet is that it is disconnected the program does not need a persistent connection to the data source to work with data in a DataSet

Instead, the program connects to the data source to populate the DataSet but disconnects from the data source immediately after retrieving the desired data

DATABASE, SQL AND ADO .NET 22

Visual Basic 2005

Programming with ADO .NETProgramming with ADO .NET

The IDE provides visual programming tools and wizards that simplify accessing data in your projects

These tools establish database connections and create the ADO.NET objects necessary to view and manipulate the data through GUI controls

DATABASE, SQL AND ADO .NET 23

Visual Basic 2005

Programming with ADO .NETProgramming with ADO .NET

Viewing a data source listed in the Data Sources window

DATABASE, SQL AND ADO .NET 24

Visual Basic 2005

Data Sources window

Programming with ADO .NETProgramming with ADO .NET

The technique through which GUI controls are connected to data sources is known as data binding

The IDE allows controls, such as a DataGridView, to be bound to a data source, such as a DataSet that represents a table in a database

Any changes you make through the application to the underlying data source will automatically be reflected in the way the data is presented in the data-bound control

DATABASE, SQL AND ADO .NET 25

Visual Basic 2005

Programming with ADO .NETProgramming with ADO .NET

Data binding architecture used to display the Authors table of the Books database in a GUI

DATABASE, SQL AND ADO .NET 26

Visual Basic 2005

Programming with ADO .NETProgramming with ADO .NET

Auto-generated code for displaying data from a database table in a DataGridView control

DATABASE, SQL AND ADO .NET 27

Visual Basic 2005

Public Class FrmDisplayTable

Private Sub AuthorsBindingNavigatorSaveItem_Click( _ ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AuthorsBindingNavigatorSaveItem.Click Me.Validate() Me.AuthorsBindingSource.EndEdit() Me.AuthorsTableAdapter.Update(Me.BIBLIODataSet.Authors) End Sub

Private Sub FrmDisplayTable_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ‘TODO: This line of code loads data into the ‘BIBLIODataSet.Authors’ ‘table. You can move, or remove it, as needed. Me.AuthorsTableAdapter.Fill(Me.BIBLIODataSet.Authors) End Sub

End Class

Public Class FrmDisplayTable

Private Sub AuthorsBindingNavigatorSaveItem_Click( _ ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AuthorsBindingNavigatorSaveItem.Click Me.Validate() Me.AuthorsBindingSource.EndEdit() Me.AuthorsTableAdapter.Update(Me.BIBLIODataSet.Authors) End Sub

Private Sub FrmDisplayTable_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ‘TODO: This line of code loads data into the ‘BIBLIODataSet.Authors’ ‘table. You can move, or remove it, as needed. Me.AuthorsTableAdapter.Fill(Me.BIBLIODataSet.Authors) End Sub

End Class

Programming with ADO .NETProgramming with ADO .NET

Displaying the Authors table in a DataGridView

DATABASE, SQL AND ADO .NET 28

Visual Basic 2005

DATABASE, SQL AND ADO .NET 29

Visual Basic 2005

DATABASE, SQL AND ADO .NET 30

Visual Basic 2005


Recommended