Visual Basic 2005
VIII. DATABASE, SQL AND ADO .NET

MELJUN CORTES
In this chapter you will learn:
•The relational database model.
•To write basic database queries in SQL.
•To add data sour...
Database, SQL and ADO .NET

A

database is an organized collection of
data
Many strategies exist for organizing data
to ...
Database, SQL and ADO .NET

Today's

most popular database systems are
relational databases
A language called SQL pronou...
Database, SQL and ADO .NET

A

relational database is a logical
representation of data that allows the
data to be accesse...
Database, SQL and ADO .NET

Common
SQL Keyword

SQL keywords
Description

SELECT

Retrieves data from one or more tables....
Database, SQL and ADO .NET

A

SQL query "selects" rows and columns
from one or more tables in a database
Such selection...
Database, SQL and ADO .NET

When

users search a database for rows
that satisfy certain selection criteria
(formally call...
Database, SQL and ADO .NET

The

rows in the result of a query can be
sorted into ascending or descending order
by using ...
Database, SQL and ADO .NET

Database

designers typically normalize
databases i.e., split related data into
separate tabl...
Database, SQL and ADO .NET

An

INNER JOIN merges rows from two
tables by testing for matching values in a
column that is...
Database, SQL and ADO .NET

The

INSERT statement inserts a row into
a table
The basic form of this statement is:
INSERT...
Database, SQL and ADO .NET

An

UPDATE statement modifies data in a
table
The basic form of the UPDATE statement
is:
UPD...
Database, SQL and ADO .NET

A

DELETE statement removes rows from a
table
The basic form of a DELETE statement is:
DELET...
Database, SQL and ADO .NET

The

ADO.NET object model provides an
API for accessing database systems
programmatically
AD...
Database, SQL and ADO .NET

ADO

.NET Architecture

ADO .NET Object Model
DATABASE, SQL AND ADO .NET

Visual Basic 2005

...
Database, SQL and ADO .NET

Namespace

System.Data is the root
namespace for the ADO.NET API
The other important ADO.NET...
Database, SQL and ADO .NET

Namespace

System.Data.OleDb contains
classes that are designed to work with any
data source
...
Database, SQL and ADO .NET

An

object of class SqlCommand (namespace
System.Data.SqlClient) represents a SQL
command tha...
Database, SQL and ADO .NET

Class

DataTable (namespace
System.Data) represents a table of data
A DataTable contains a c...
An

object of class System.Data.DataSet,
which consists of a set of DataTables and
the relationships among them, represen...
An

advantage of using class DataSet is
that it is disconnected the program does
not need a persistent connection to the
...
The

IDE provides visual programming
tools and wizards that simplify accessing
data in your projects
These tools establi...
Viewing

a data source listed in the Data
Sources window

Data Sources
window

Programming with ADO .NET
DATABASE, SQL AN...
The

technique through which GUI controls
are connected to data sources is known as
data binding
The IDE allows controls...
Data

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

Programming with ADO .NET
DA...
Auto-generated

code for displaying data
from a database table in a DataGridView
control
Public Class FrmDisplayTable
Pub...
Displaying

the Authors table in a
DataGridView

Programming with ADO .NET
DATABASE, SQL AND ADO .NET

Visual Basic 2005
...
DATABASE, SQL AND ADO .NET

Visual Basic 2005

29
DATABASE, SQL AND ADO .NET

Visual Basic 2005

30
Upcoming SlideShare
Loading in …5
×

MELJUN CORTES Visual Basic 2005 Database SQL and ADO NET

304 views

Published on

MELJUN CORTES Visual Basic 2005 Database SQL and ADO NET

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
304
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

MELJUN CORTES Visual Basic 2005 Database SQL and ADO NET

  1. 1. Visual Basic 2005 VIII. DATABASE, SQL AND ADO .NET MELJUN CORTES
  2. 2. 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. Database, SQL and ADO .NET Objectives DATABASE, SQL AND ADO .NET Visual Basic 2005 2
  3. 3. Database, SQL and ADO .NET 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 Overview DATABASE, SQL AND ADO .NET Visual Basic 2005 3
  4. 4. Database, SQL and ADO .NET 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 Overview DATABASE, SQL AND ADO .NET Visual Basic 2005 4
  5. 5. Database, SQL and ADO .NET 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 Relational Databases DATABASE, SQL AND ADO .NET Visual Basic 2005 5
  6. 6. Database, SQL and ADO .NET Common SQL Keyword SQL keywords 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. SQL DATABASE, SQL AND ADO .NET Visual Basic 2005 6
  7. 7. Database, SQL and ADO .NET 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: SELECT * FROM tableName SELECT * FROM tableName The asterisk (* ) indicates that all the columns from the tableName table should be retrieved SELECT column1, column2,… FROM tableName SELECT column1, column2,… FROM tableName Column list separated by comma (, ) SQL DATABASE, SQL AND ADO .NET Visual Basic 2005 7
  8. 8. Database, SQL and ADO .NET When users search a database for rows that satisfy certain selection criteria (formally called predicates), only rows that satisfy the selection criteria are selected SELECT column1, column2,… FROM tableName WHERE criteria SELECT column1, column2,… FROM tableName WHERE criteria SELECT Title, EditionNumber, Copyright SELECT Title, EditionNumber, Copyright FROM Titles FROM Titles WHERE Copyright > '2004' WHERE Copyright > '2004' Retrieve rows where Copyright is greater than ‘2004’ SQL DATABASE, SQL AND ADO .NET Visual Basic 2005 8
  9. 9. Database, SQL and ADO .NET The rows in the result of a query can be sorted into ascending or descending order by using the optional ORDER BY clause Optional SELECT column1, column2,… FROM tableName ORDER BY columnList ASC SELECT column1, column2,… FROM tableName ORDER BY columnList ASC Or Or SELECT column1, column2,… FROM tableName ORDER BY columnList DESC SELECT column1, column2,… FROM tableName ORDER BY columnList DESC SELECT AuthorID, FirstName, LastName SELECT AuthorID, FirstName, LastName FROM Authors FROM Authors ORDER BY LastName ORDER BY LastName SQL DATABASE, SQL AND ADO .NET Visual Basic 2005 9
  10. 10. Database, SQL and ADO .NET 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 SQL DATABASE, SQL AND ADO .NET Visual Basic 2005 10
  11. 11. Database, SQL and ADO .NET 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: SELECT columnName1, columnName2, ...  SELECT columnName1, columnName2, ...  FROM table1 INNER JOIN table2  FROM table1 INNER JOIN table2      ON table1.columnName = table2.columnName     ON table1.columnName = table2.columnName SELECT FirstName, LastName, ISBN  SELECT FirstName, LastName, ISBN  FROM Authors INNER JOIN AuthorISBN  FROM Authors INNER JOIN AuthorISBN       ON Authors.AuthorID = AuthorISBN.AuthorID       ON Authors.AuthorID = AuthorISBN.AuthorID  ORDER BY LastName, FirstName 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 SQL DATABASE, SQL AND ADO .NET Visual Basic 2005 11
  12. 12. Database, SQL and ADO .NET The INSERT statement inserts a row into a table The basic form of this statement is: INSERT INTO tableName ( columnName1, columnName2, ..., columnNameN )  INSERT INTO tableName ( columnName1, columnName2, ..., columnNameN )  VALUES ( value1, value2, ..., valueN ) VALUES ( value1, value2, ..., valueN ) INSERT INTO Authors ( FirstName, LastName )  INSERT INTO Authors ( FirstName, LastName )  VALUES ( 'Sue', 'Smith' )  VALUES ( 'Sue', 'Smith' )  SQL DATABASE, SQL AND ADO .NET Visual Basic 2005 12
  13. 13. Database, SQL and ADO .NET An UPDATE statement modifies data in a table The basic form of the UPDATE statement is: UPDATE tableName  UPDATE tableName  SET columnName1 = value1,  SET columnName1 = value1,      columnName2 = value2, ...,      columnName2 = value2, ...,      columnNameN = valueN      columnNameN = valueN  WHERE criteria WHERE criteria UPDATE Authors  UPDATE Authors  SET LastName = 'Jones'  SET LastName = 'Jones'  WHERE LastName = 'Smith' AND FirstName = 'Sue' WHERE LastName = 'Smith' AND FirstName = 'Sue' SQL DATABASE, SQL AND ADO .NET Visual Basic 2005 13
  14. 14. Database, SQL and ADO .NET A DELETE statement removes rows from a table The basic form of a DELETE statement is: DELETE FROM tableName WHERE criteria  DELETE FROM tableName WHERE criteria  DELETE FROM Authors  DELETE FROM Authors  WHERE LastName = 'Jones' AND FirstName = 'Sue'  WHERE LastName = 'Jones' AND FirstName = 'Sue'  SQL DATABASE, SQL AND ADO .NET Visual Basic 2005 14
  15. 15. Database, SQL and ADO .NET 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 ADO .NET Object Model DATABASE, SQL AND ADO .NET Visual Basic 2005 15
  16. 16. Database, SQL and ADO .NET ADO .NET Architecture ADO .NET Object Model DATABASE, SQL AND ADO .NET Visual Basic 2005 16
  17. 17. Database, SQL and ADO .NET 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 ADO .NET Object Model DATABASE, SQL AND ADO .NET Visual Basic 2005 17
  18. 18. Database, SQL and ADO .NET 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 ADO .NET Object Model DATABASE, SQL AND ADO .NET Visual Basic 2005 18
  19. 19. Database, SQL and ADO .NET 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 ADO .NET Object Model DATABASE, SQL AND ADO .NET Visual Basic 2005 19
  20. 20. Database, SQL and ADO .NET 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 ADO .NET Object Model DATABASE, SQL AND ADO .NET Visual Basic 2005 20
  21. 21. 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 ADO .NET Object Model DATABASE, SQL AND ADO .NET Visual Basic 2005 21
  22. 22. 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 ADO .NET Object Model DATABASE, SQL AND ADO .NET Visual Basic 2005 22
  23. 23. 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 Programming with ADO .NET DATABASE, SQL AND ADO .NET Visual Basic 2005 23
  24. 24. Viewing a data source listed in the Data Sources window Data Sources window Programming with ADO .NET DATABASE, SQL AND ADO .NET Visual Basic 2005 24
  25. 25. 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 Programming with ADO .NET DATABASE, SQL AND ADO .NET Visual Basic 2005 25
  26. 26. Data binding architecture used to display the Authors table of the Books database in a GUI Programming with ADO .NET DATABASE, SQL AND ADO .NET Visual Basic 2005 26
  27. 27. Auto-generated code for displaying data from a database table in a DataGridView control Public Class FrmDisplayTable Public Class FrmDisplayTable Private Sub AuthorsBindingNavigatorSaveItem_Click( _ Private Sub AuthorsBindingNavigatorSaveItem_Click( _ ByVal sender As System.Object, ByVal e As System.EventArgs) ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AuthorsBindingNavigatorSaveItem.Click Handles AuthorsBindingNavigatorSaveItem.Click Me.Validate() Me.Validate() Me.AuthorsBindingSource.EndEdit() Me.AuthorsBindingSource.EndEdit() Me.AuthorsTableAdapter.Update(Me.BIBLIODataSet.Authors) Me.AuthorsTableAdapter.Update(Me.BIBLIODataSet.Authors) End Sub End Sub Private Sub FrmDisplayTable_Load(ByVal sender As System.Object, _ Private Sub FrmDisplayTable_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ByVal e As System.EventArgs) Handles MyBase.Load ‘TODO: This line of code loads data into the ‘BIBLIODataSet.Authors’ ‘TODO: This line of code loads data into the ‘BIBLIODataSet.Authors’ ‘table. You can move, or remove it, as needed. ‘table. You can move, or remove it, as needed. Me.AuthorsTableAdapter.Fill(Me.BIBLIODataSet.Authors) Me.AuthorsTableAdapter.Fill(Me.BIBLIODataSet.Authors) End Sub End Sub End Class End Class Programming with ADO .NET DATABASE, SQL AND ADO .NET Visual Basic 2005 27
  28. 28. Displaying the Authors table in a DataGridView Programming with ADO .NET DATABASE, SQL AND ADO .NET Visual Basic 2005 28
  29. 29. DATABASE, SQL AND ADO .NET Visual Basic 2005 29
  30. 30. DATABASE, SQL AND ADO .NET Visual Basic 2005 30

×