Course Slides Link - Talk-IT
Download
Report
Transcript Course Slides Link - Talk-IT
17. Data Access
ADO.Net Architecture
New
Features of ADO.NET
Visual Studio Visual Studio Wizards and
Designers
Demonstration: Creating Data Components
Lab 17: Accessing Data Using ADO.NET
Code Samples
1
ADO.NET Architecture
What
Is ADO.NET?
What Is a Connected Environment?
What Is a Disconnected Environment?
What Is the ADO.NET Object Model?
What Is the DataSet Class?
What Is the .NET Data Provider?
2
What Is ADO.NET?
ADO.NET is a data access technology. It provides:
A set of classes, interfaces, structures, and
enumerations that manage data access from within
the .NET Framework
An evolutionary, more flexible successor to ADO
A system designed for disconnected environments
A programming model with advanced XML support
3
What Is a Connected
Environment?
A connected
environment is one in which users are
constantly connected to a data source
Advantages:
Environment is easier to secure
Concurrency is more easily controlled
Data is more likely to be current than in other scenarios
Disadvantages:
Must have a constant network connection
Scalability
4
What Is a Disconnected Environment?
In a disconnected environment, a subset of data from
a central data store can be copied and modified
independently, and the changes merged back into
the central data store
Advantages
You can work at any time that is convenient for you, and can
connect to a data source at any time to process requests
Other users can use the connection
A disconnected environment improves the scalability and
performance of applications
Disadvantages
Data is not always up to date
Change conflicts can occur and must be resolved
5
What Is the ADO.NET Object Model?
DataSet
.NET Data Provider
Connection
Transaction
DataAdapter
SelectCommand
DataTableCollection
DataTable
DataRowCollection
InsertCommand
DataColumnCollection
Parameters
UpdateCommand
ConstraintCollection
DataReader
DeleteCommand
Command
DataRelationCollection
XML
Database
6
What Is the .NET Data Provider?
Database
Connection
Manages the connection
to a database
Executes a query command
on the database
Command
Exchanges data between
the data set and the database
DataAdapter
DataReader
Provides efficient access to a
stream of read-only data
7
Creating an Application That Uses
ADO.NET to Access Data
How
to Specify the Database Connection
How to Specify the Database Command
How to Create the DataAdapter Object
How to Create a DataSet Object
How to Bind a DataSet to a DataGrid
How to Use the Data Wizards in Visual Studio
.NET
8
How to Specify the Database
Connection
Use
the Connection object to:
Choose the connection type
Specify the data source
Open the connection to the data source
Use
the connection string to specify all of the options
for your connection to the database, including the
account name, database server, and database name
string connectionStr = @"Data Source=localhost;
Integrated Security=SSPI; Initial Catalog=northwind";
9
How to Specify the Database
Command
string commandStr=@"SELECT CustomerName,
CompanyName FROM Customers";
Create a string containing SQL statements
Remember that Verbatim strings can make this much easier!
Examples of SQL statements:
SELECT * FROM Customers
SELECT CustomerName FROM Customers
SELECT * FROM Customers WHERE Country = 'Mexico'
10
How to Create the DataAdapter
Object
DataSet
Data source
DataAdapter
DataTable
Fill
Update
DataAdapter
DataTable
Fill
Update
11
What Is the DataSet Class?
DataSets
consist of one
or more tables and
relations
DataSet
Tables
Table
Loaded
from one or more
data adapters
Created as you work
Loaded from XML
Loaded from other
DataSets
Columns
Column
Constraints
Constraint
Rows
Tables
contain columns,
constraints, and rows
Row
Relations
All
Relation
Object
are collections!
Collection
12
How to Bind a DataSet to a
DataGrid
To bind programmatically
DataGrid dataGrid1 = new DataGrid();
sqlDataAdapter1.Fill(dataSet1, "Customers");
sqlDataAdapter2.Fill(dataSet1, "Orders");
dataGrid1.DataSource = dataSet1;
13
Changing Database Records
How
to Access Data in a DataSet Object
How to Update a Database in ADO.NET
How to Create a Database Record
How to Update a Database Record
How to Delete a Database Record
14
How to Access Data in a DataSet
Object
DataRow
objects
DataTable
objects
DataColumn
objects
DataColumn
objects
15
How to Update a Database in
ADO.NET
Client
Server
DataAdapter
DataSet
Data
Database
Fill
DataTable
Update
Data
InsertCommand
UpdateCommand
DeleteCommand
16
How to (Manually) Create a Database
Record
Create a new row that matches the table schema
DataRow myRow = dataTable.NewRow();
Add the new row to the dataset
dataTable.Rows.Add( myRow );
Update the database
sqlDataAdapter1.Update( dataSet );
17
How to (Manually) Delete a Database
Record
Delete
the row from the dataset
dataTable.Rows[0].Delete();
Update
the database
dataAdapter.Update(dataSet);
Accept
the changes to the dataset
dataSet.AcceptChanges();
18
Data Reader
Data Readers manage the stream of results from a
SELECT statement or Stored Procedure
Data Readers are read only
Data Readers only move forwards through the data –
they cannot go backwards
No other database operation can be performed while
a data reader is open
19
Using a DataReader with a SELECT
Statement
string queryString =
"SELECT FirstName, LastName FROM dbo.Employees";
//Create the command objects
SqlCommand com = new SqlCommand(queryString, conn);
//Call the command's ExecuteReader method
SqlDataReader dr = com.ExecuteReader();
//Loop through the datareader to output the employee names
while (dr.Read())
{
listBox1.Items.Add(dr.GetString(0) + " " +
dr.GetString(1));
}
20
Using a DataReader to call a Stored
Procedure
//Create the command object and set its properties
SqlCommand com = new SqlCommand();
com.Connection = conn;
com.CommandText = "CustOrderHist";
com.CommandType = CommandType.StoredProcedure;
//Create the parameter object and add it to the command's
collection
SqlParameter param =
new SqlParameter("@CustomerID", SqlDbType.NVarChar, 5);
param.Value = txtID.Text;
com.Parameters.Add(param);
21
Creating a Two Tier Application
Create
the data tier
Create
a new class library project
Create a class to represent a business entity, say
customers
Add methods that access the database, using ADO.Net
Add properties that expose attributes, say name, phone …
Create
the presentation tier
Create
a new Windows Application project
Add a reference to the above data project
Create an object from class
Use the object’s properties to populate controls on a form
Use the object’s methods to access the database
22
New Features of ADO.NET 2.0
Multiple
active result sets
One
connection can host more than one result set
Reduces number of connections on a database
Asynchronous
operations
Begin
a data access operation, continue with other tasks,
and then later end the original operation
Batch
updates
Update
multiple rows in one batch
Use the UpdateBatchSize property of the DataAdapter
23
Visual Studio Wizards and Designers
Data
Source Configuration Wizard
TableAdapter Configuration Wizard
TableAdapter Query Configuration Wizard
Preview Data Dialog Box
24
Lab 17: Accessing Data Using ADO.NET
Exercise 1: Retrieving Data into a Data
Set
Exercise 2: Updating the Database
from a Data Set
Exercise 3: Retrieving Data with a
Data Reader
Exercise 4: Calling a Stored Procedure
25