Basic Structural Concepts of .NET
Download
Report
Transcript Basic Structural Concepts of .NET
.NET Programming: Databases
& ADO.NET
Christopher M. Pascucci
Database Basics
A relational database is a tool for storing and manipulating information
efficiently and effectively in the sense that data is protected from
accidental loss or corruption, that it doesn't use more resources (human
or computer) than necessary.
A relational database uses tables to store and manipulate data.
Each table contains one or more records, or rows, that contain
information about a single entry.
Records contain a set of related fields that are used to store data.
Each record contains one or more fields, or columns, that contain a
single piece of data.
Most tables include a primary key field that uniquely identifies each
record (row) in a table.
Storing students (records) by name in a large university will make it difficult to find
students with the same name. Giving each student a StudentID uniquely identifies
each student.
SSN uniquely identify each American in the U.S.
Database Basics
The tables in a relational database are related to each other
through their key columns. A column that identifies a related
row in another table is called a foreign key.
Table
SQL Concepts
Structured Query language (SQL) is an English – like language that is
used to manipulate data in the database.
There are 4 basic SQL statements that are needed to work with data in
a database.
SELECT – used to query a table and find specific information based on some criteria.
UPDATE – used for modifying data in one or more rows (records) in a table.
INSERT – used to add one or more rows (records) to a table.
DELETE – used to delete one or more rows (records) in a table.
Each Database Management System (DBMS) may have a slightly
different version of SQL called a dialect that was derived from the
standard language.
SQL SELECT Syntax
The result of a Select statement is called a result set, which is a logical
set of rows that consists of all of the columns and rows requested by the
Select statement.
Select column_name [, column_name2]...
From table_name
[Where selection-criteria]
[Order By column_name [Asc|Desc] [, column_name2[Asc|Desc]]...]
Example:
Select ProductID, Name, UnitPrice
From Products
Where CategoryID = 'Props'
Order By UnitPrice
Note: Use (*) to select all columns.
Select * From Products
Result Set shown above.
Click button to view full table.
SQL INSERT Syntax
The Insert statement is used to add a new row (record) to a table. It can
be used to set fields to specified values as it adds the new record.
Insert [Into] table_name [(column_name-list)]
Values (values-list)
Example:
Insert [Into] Products (ProductID, Description, UnitPrice, OnHand)
Values (“abc01”,”Toy”,”$19.99”,”50”)
Note: Order of column-list items and value-list items
is very important…!
Click button to view full table.
SQL UPDATE Syntax
The Update statement is used to modify/edit existing data.
Update table_name
Set expression [, expression2]...
[Where selection-criteria]
Example:
Update Products
Set UnitPrice = “25.79”
Where ProductID = “arm01”
Click button to view full table.
SQL DELETE Syntax
The Delete statement is used to remove a row or rows from a table.
Delete [From] table_name
[Where selection-criteria]
Example:
Delete From Products
Where ProductID = “arm01”
Click button to view full table.
SQL Example Table
The full table used in SQL examples.
Return to last
viewed slide
ADO.NET Introduction
ADO.NET is the primary data access API for the .NET Framework.
It provides the classes that are used to create applications that use a
database.
These classes become objects you can use in your application to manage and use
database connections.
To use these classes in the .NET Framework you must import the namespace
System.Data that contains all these useful classes.
Data that is used in your application is stored in a Dataset, which
contains one or more Data Tables.
Data in the Dataset is independent of the database that was used to retrieve it.
The connection to the database is typically closed after data is retrieved from the
database. The connection is opened again when it’s needed.
A Data Adapter is used to load data into the Data Table.
Data Adapter’s major function is to manage the exchange of data between the
Dataset and database.
ADO.NET Explained
Dataset:
Data in the Dataset is independent of the database that was used to retrieve it.
The connection to the database is typically closed after data is retrieved from the
database. The connection is opened again when it’s needed.
Since the application actually works with a copy of data from the database it is called
a Disconnected Data Architecture because of its “disconnected” nature.
Data Adapter:
Manages the exchange of data between the Dataset and a database.
Issues an SQL statement (Select, Insert, Update or Delete) that is stored in the
command object.
The command object uses a connection object to connect to the database and
retrieve or update the data.
The data is transferred back to the Data Adapter, which then stores it in a Dataset
that can be used by the application.
ADO.NET Explained
Dataset
.NET data provider
Data table
Data adapter
Command
Database server
Connection
Database
ADO.NET Explained
An ADO.NET data provider connects to a data source such as SQL
Server, Oracle, or an OLE DB data source, and provides a way to
execute commands against that data source.
The ADO.NET classes responsible for working directly with a database
are provided by the .NET Data Providers:
The .NET Framework includes data
providers for SQL Server, Oracle, OLE
DB, ODBC, etc…
The Data Providers included in the .NET
Framework contain the same objects,
although their names and some of their
properties and methods are different.
• For example, the SQL Server version of a
database connection is the
SqlConnection, while the OLE DB version
is an OleDbConnection.
ADO.NET Programming
The following is an example of code needed to establish a connection and
retrieve data from a database.
Dim strSQL As String
Dim strConnection As String
Dim objDataSet As New DataSet()
Dim objAdapter As OleDbDataAdapter
Dim objConnection As OleDbConnection
Dataset used to hold data retrieved from the DB.
Using OleDb Provider objects
strConnection =
"provider=SQLOLEDB.1;server=dwarf.cis.temple.edu;Database=fa08_c4
30901nn;User id=fa08_c430901nn;Password=yourpassword“
Connection object used to
strSQL = "SELECT * from Product"
create a connection to the
objConnection = New OleDbConnection(strConnection)
DB.
objAdapter = New OleDbDataAdapter(strSQL, objConnection)
objAdapter.Fill(objDataSet)
DataAdapter used to manage
Fills a Dataset with data
retrieved from the DB.
a connection, exchange of
data and issue SQL
commands to the DB.
Connected vs. Disconnected Data
ADO.NET contains two different approaches to work with data in a
database: Connected Data Architecture and the Disconnected Data
Architecture.
Connected Data Architecture:
Represents the objects that insist on having an open connection available for them to
work and interact with the data source.
ADO.NET provides classes to communicate directly with a data source.
Disconnected Data Architecture:
Represents the objects that open and close a connection to the data source as
needed.
Your application works with data that was copied from the data source. Any changes
made to the “copied” data in the Dataset will be later reconciled with the data source.
Connected applications alone don't fulfill the demands of today’s distributed
applications.
Improved performance, more efficient and more complex than the connected
approach.
Disconnected Data Architecture
The major advantage of the disconnected approach is that it improves
system performance due to the fact that it uses less resources for
maintaining connections when working with a data source.
However, there is a disadvantage to this approach that occurs when two
or more users try to update the same row of a table, which is called a
concurrency problem.
This problem occurs because once data is retrieved from the database, the
connection is dropped. As a Database
result,server
the DBMS can’t manage the update process.
There are two ways that ADO.NET can handle concurrency problems:
User 1
DBMS
1. Optimistic
Concurrency – The
program checks toUser
see2 if there has been a change
Products
Products If it has, the update
Products
to the
row since it was retrieved.
or deletion will be refused
data table
table
data table
and a concurrency exception will be thrown that your program must handle.
2. “Last In Wins” – Since no checking is done, the row that was updated by the last
user will overwrite changes made to the row by the previous user.
Another way to avoid concurrency problems is to retrieve and update only one row at
a time.
ADO.NET Terms Summary
Dataset:
Data in the Dataset is independent of the database that was used to retrieve it.
Data Adapter:
Manages the exchange of data between the Dataset and a database.
Allowing a DataSet and DataTable to be filled from the data source and later reconciled with the
data source.
Connection:
Connects to the data source.
Command:
Executes commands (SQL statements) against the data source.
Data Providers:
The ADO.NET classes responsible for working directly with a database.
Data Reader
Retrieves query results in a read-only, forward-only connected result set.
SQL Server Data Types
Large value data types: varchar(max), nvarchar(max), and varbinary(max)
Large object data types: text, ntext, image, varchar(max), nvarchar(max),
varbinary(max), and xml
For more information on SQL Server Data Types on the MSDN website.
Character Strings:
Use char when the sizes of the column data entries are consistent.
Use varchar when the sizes of the column data entries vary considerably.
Use varchar(max) when the sizes of the column data entries vary considerably, and the size might
exceed 8,000 bytes.
Use text when there is a variable-length of non-unicode data .
Unicode Character Strings:
Use nchar when the sizes of the column data entries are probably going to be similar.
Use nvarchar when the sizes of the column data entries are probably going to vary considerably.
Use ntext when there is a variable-length of unicode data.
Exact Numbers:
Use tinyint when the numbers range from 0 – 255 (1-byte of storage)
Use smallint when the numbers range from -32,768 to 32,767 (2-bytes of storage)
Use int when the numbers range from -2,147,483,648 to 2,147,483,647 (4-bytes of storage)
Use bigint when the numbers range from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807 (8-bytes of storage)