0% found this document useful (0 votes)
44 views

Kitaw A

This document provides an overview of ADO.NET objects for accessing and manipulating data in .NET applications. It describes provider objects specific to different data sources that handle connections, commands, and data readers. It also covers consumer objects like DataSet and DataTable that allow working with data in memory. Key classes covered include Connection, Command, DataReader, DataAdapter, DataSet and DataTable. The document provides examples of creating and using instances of these classes with SQL Server and OLE DB data sources.

Uploaded by

world channel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Kitaw A

This document provides an overview of ADO.NET objects for accessing and manipulating data in .NET applications. It describes provider objects specific to different data sources that handle connections, commands, and data readers. It also covers consumer objects like DataSet and DataTable that allow working with data in memory. Key classes covered include Connection, Command, DataReader, DataAdapter, DataSet and DataTable. The document provides examples of creating and using instances of these classes with SQL Server and OLE DB data sources.

Uploaded by

world channel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Data Access with ADO.

NET

Kitaw A.
ADO.NET
ADO.NET is the name for the set of classes you use to access data in a
relational, table-oriented format
This includes relational databases such as Microsoft Access and SQL
Server, as well as other databases
ADO.NET is integrated into the NET Framework and is designed to be used
with any .NET language, especially C#
It is located in the System.Data namespace
ADO.NET takes its name from ADO (ActiveX Data Objects), a widely used
set of classes used for data access in the previous generation of Microsoft
technologies
Overview of ADO.NET Objects
We divide the classes into .NET data provider objects and consumer objects.
Provider objects are specific to each type of data source - the actual reading
and writing to and from the data source is done with the provider-specific
objects
Consumer objects are what you use to access and manipulate the data once
you have read it into memory
Provider Objects
These are the objects defined in each .NET data provider
The names are prefaced with a name unique to the provider:
For example:
the actual connection object for the Oracle DB provider is
OracleConnection
the class for the SQL Server .NET provider is SqlConnection
These objectes are defined in provider specific namespaces
For example: Provider objects for SQL Server data source are
defined in System.Data.SqlClient namespace
Provider Objects: Connection
The connection object is the first object that you will typically
use, before using most of the other ADO.NET objects
it provides the basic connection to your data source
If you are using a database that requires a username and
password, or one on a remote network server, the connection
object takes care of the details of establishing the connection and
logging in
Provider Objects: Command
You use this object to give a command such as a SQL query to a
data source, such as "SELECT * FROM Movie" to query the data
in the Movie table
The provider-specific names include SqlCommand for SQL
Server and OleDbCommand for OLE DB
Provider Objects: CommandBuilder
This object is used to build SQL commands for data modification
from objects based on a single-table query
The provider-specific names include SqlCommandBuilder for
SQL Server and OleDbCommandBuilder for OLE DB.
Provider Objects: DataReader
This is a fast, simple-to-use object that reads a forward-only read-
only stream of data
This object gives the maximum performance for simply reading
data;
The provider-specific names include SqlDataReader for SQL
Server and OleDbDataReader for OLE DB.
Provider Objects: DataAdapter
This is a general-purpose class that performs various operations
specific to the data source, including updating changed data,
filling DataSet objects and other operations
The provider-specific names include SqlDataAdapter for SQL
Server and OleDbAdapter for OLE DB
Consumer Objects
These are the objects defined for the disconnected, consumer side
of ADO.NET
These aren't related to any specific .NET data provider
Defined within the System.Data namespace:
Consumer Objects: DataSet
The DataSet is used to hold a set of data within our program in
table format
It doesn't care where that data actually comes from
The DataSet has features that let you access lower-level objects
that represent individual tables (DataTable)
Consumer Objects: DataTable
This object represents one of the tables in the DataSet, such as
Movie, Customer
The DataTable object has features that allow you to access its
rows and columns
DataColumn object - this represents one column in the table, for
example MovieTitle or CustomerPhoneNumber
DataRow object - this represents one row of related data from a
table; for example a particular customer’s record
Working with ADO.NET Objects
The first step in using ADO.NET within your C# code is to reference the
System.Data namespace, in which all the ADO.NET consumer classes are
located
using System.Data;
Then use the .NET Data Provider you want to work with
SQL Server .NET Data Provider
If you are using SQL Server (version 7 or greater), the best performance and most
direct access to the underlying features is available with the SQL Server-
specific .NET data provider, referenced with this using directive:
using System.Data.SqlClient;
Working with ADO.NET Objects
Then use the .NET Data Provider you want to work with
OLE DB .NET Data Provider
For most data sources other than SQL Server (such as Microsoft Access,
versions of SQL Server earlier than version 7, and others) you'll use the
OLE DB .NET data provider, referenced with this using directive:
using System.Data.OleDb;
Connection Object
It provides the basic connection to your data source
The connection object takes care of the details of establishing the
connection and logging in
SqlConnection: for SQL Server
OleDbConnection: for OLE DB (e.g. Microsoft Access)
Common Properties:
State: Current state of the connection based on ConnectionState enumeration:
The ConnectionState enumeration has the following values:
Open
Closed
And so on
Connection Object
Common Properties:
ConnectionString: Gets or sets the string used to establish database connection
Common Methods:
Open: establishes database connection
Close: Closes a databases connection
Connection Object: SQL Server
Instantiate the SqlConnection as shown below:
SqlConnection c = new SqlConnection(“Data Source=.\\
SQLEXPRESS;Integrated Security=SSPI;Initial
Catalog=testdb”);
The connection string consists of named entries separated by semicolons;
Data Source: the name of the SQL Server instance you are accessing
Integrated Security: specifies how to log in to the database
SSPI (Security Support Provider Interface) is the integrated security of the Windows
login
Initial Catalog: name of the database you want to use
Connection Object: OleDb
Instantiate the OleDb as shown below:
OleDbConnection c = new
OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;D
ata Source=testdb.accdb");

The connection string consists of named entries separated by semicolons;


Data Source: the path to Access database file
Provider: specifies OLEDB .NET Data Provider to use
Connection Object
Once we have a connection object, we can establish the connection to the
database:
c.Open();
After we done working with the database it is a good practice to close the
connection
c.Close();
Command Object
Used to execute queries based on the connection established
Common properties:
CommandText: a string containing the SQL query
Connection: Gets or sets the connection object associated with the command
Common methods:
ExecuteNonQuery: executes a query (insert, update, delete, alter) and returns the
number of rows affected
ExecuteReader: excutes a query (select) and builds DataReader object
Command Object: SQL Server
Instantiate SQLCommand using the following constructor:
string sql=“insert/update/delete query”;
SqlCommand command=new SqlCommnad(sql,connection
object);
After the command object is established commands can be executed as
shown below:
command.ExecuteNoneQuery();
Command Object: SQL Server
Instantiate SQLCommand using the following constructor:
string sql=“select query”;
SqlCommand command=new SqlCommnad(sql,connection
object);
After the command object is established commands can be executed as
shown below:
SqlDataReader reader= command.ExecuteReader();
DataReader: SQL Server
This object gives the maximum performance for simply reading data
Provides important properties and methods to read and manipulate data
Property:
HasRows: determines whether the reader contains one or more records
Methods:
Read: advances the reader to the next record and returns true if there is a record to
read otherwise it returns false
Close: closes the reader
The data reader object provides an indexer property
The indexer is overloaded, and allows you to reference the columns as an
array reference by column name or by an integer
DataReader: SQL Server
Example:
string sql=“select title,type from movie”;
SqlCommand command=new SqlCommnad(sql,connection
object);
After the command object is established commands can be executed as
shown below:
SqlDataReader reader = command.ExecuteReader();
while(reader.Read()){
listBox1.Items.Add(reader[0]+“: ”+reader[1]);
}
DataAdapter
This is a general-purpose class that performs various operations specific to
the data source, retrieving and updating data, filling DataSet and
DataTable objects and so on
The provider-specific names include SqlDataAdapter for SQL Server and
OleDbDataAdapter for OLE DB
SQL Server
SqlDataAdapter a=new SqlDataAdapter(“sql
statement”,connection object);
DataSet
The DataSet is the central object in ADO.NET
A DataSet contains a set of DataTable objects representing the database
tables that you are working with
If you are working with two or more tables in your program you must use a
DataSet object
If you are working with single table you could go for DataTable (will be
discussed later)
DataSet and DataTable can be filled with data using Fill() method of the
data adapter
The data adapter is the object that ties the DataSet/DataTable to a
particular database
Overview of ADO.NET Classes and Objects
DataTable Object
This object represents one of the tables in the DataSet, such as Movie,
Customer
The DataTable object has features that allow you to access its rows and
columns:
DataColumn object - this represents one column in the table, for example
StudentID or StudentName
DataRow object - this represents one row of related data from a table; for
example a particular student’s StudentID, name, cgpa, and so on
DataTable is filled by data adapter
Overview of ADO.NET Classes and Objects
DataTable Object (Example):
SqlDataAdapter a=new SqlDataAdapter(“select * from
movie”,c);
DataTable movieTable=new DataTable();
a.Fill(movieTable);

You might also like