Kitaw A
Kitaw A
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");