Unit 3
Unit 3
NET
.NET Data Access and Manipulation
Unit 3
What is ADO.NET?
ADO.NET is an object-oriented set of libraries that allows you to
interact with data sources.
Data Sources OLE/DB via the Connection Managed provider calls the
object SQL APIs
Disconnected data Limited support, suitable for Strong support, with updating
R/O
Passing datasets COM marshalling DataSet support for XML
passing
Scalability Limited Disconnected access provides
scalability
ADO.NET Architecture
ADO.NET Core Objects
Object Description
Connection Establishes a connection to a specific data source. (Base
class: DbConnection)
Command Executes a command against a data source. Exposes
Parameters and can execute within the scope of a
Transaction from a Connection. (The base class:
DbCommand)
DataReader Reads a forward-only, read-only stream of data from a data
source. (Base class: DbDataReader)
DataAdapter Populates a DataSet and resolves updates with the data
source. (Base class: DbDataAdapter)
DataTable Has a collection of DataRows and DataColumns
representing table data, used in disconnected model
DataSet Represents a cache of data. Consists of a set of DataTables
and relations among them
.NET Framework data providers:
.NET Framework Data Provider for For data sources exposed by using OLE DB. Uses
OLE DB the System.Data.OleDb namespace.
.NET Framework Data Provider for For data sources exposed by using ODBC. Uses
ODBC the System.Data.Odbc namespace.
.NET Framework Data Provider for For Oracle data sources. The .NET Framework Data Provider for
Oracle Oracle supports Oracle client software version 8.1.7 and later, and
uses the System.Data.OracleClient namespace.
EntityClient Provider Provides data access for Entity Data Model (EDM) applications.
Uses the System.Data.EntityClient namespace.
.NET Framework Data Provider for SQL Provides data access for Microsoft SQL Server Compact 4.0. Uses
Server Compact 4.0. the System.Data.SqlServerCe namespace.
Core Objects of .NET Framework Data
Providers
The following table outlines the four core objects that make up
a .NET Framework data provider.
Object Description
Connection Establishes a connection to a specific data source. The base class for
all Connection objects is the DbConnection class.
DataAdapter Populates a DataSet and resolves updates with the data source. The
base class for all DataAdapter objects is the DbDataAdapter class.
In addition to the core classes listed in the table earlier in this document, a .NET
Framework data provider also contains the classes listed in the following table.
Object Description
Transaction Enlists commands in transactions at the data source. The base class for
all Transaction objects is the DbTransaction class. ADO.NET also provides
support for transactions using classes in the System.Transactions namespace.
ConnectionStringBuilder A helper object that provides a simple way to create and manage the contents of
connection strings used by the Connection objects. The base class for
all ConnectionStringBuilder objects is the DbConnectionStringBuilder class.
Parameter Defines input, output, and return value parameters for commands and stored
procedures. The base class for all Parameter objects is the DbParameter class.
Exception Returned when an error is encountered at the data source. For an error
encountered at the client, .NET Framework data providers throw a .NET
Framework exception. The base class for all Exception objects is
the DbException class.
Error Exposes the information from a warning or error returned by a data source.
ClientPermission Provided for .NET Framework data provider code access security attributes. The
base class for all ClientPermission objects is the DBDataPermission class.
Access SQL Server Databases with ADO.NET
Use classes in the System.Data.SqlClient namespace to
access and manipulate SQL Server databases
Connecting to an SQL Server Database
SqlConnection class: used to connect to an SQL Server database
Create an object from this class, passing in a connection string
Connecting to an SQL Server Database
SqlConnection con = new SqlConnection ("Data Source=.;Initial
Catalog = DatabaseConnectivity;Trusted_Connection=true;");
Eg. con.Open()
Eg. con.Close()
Creating Database in Sql Server
Step1 –Go to Server Explorer and right click data Connections to
create new sql server database
Creating Database in Sql Server
Step2- Select data source—
Microsoft SQL Server Database
File
Step3- Enter database name
then click on ok so a new
database with that name will be
created
Creating Database in Sql
Server
Step 4-Now right click Table to create
new table inside aa.mdf database
Step 5-Enter number of columns and
then save the table
Creating Database in Sql
Server
Now you can see after saving table
Table2 is created in aa.mdf
Step 6 right click table and select
show table data to enter row in table
Using Exception Handling to Control
SQL Server Errors
Place the Open() method within a try…catch block
to trap connection errors
SqlException class:
Part of the System.Data.SqlClient namespace
Represents the exception that is thrown when SQL
Server returns an error or warning
Number and Message properties provide an error code
and message for the exception
Using Exception Handling to Control SQL
Server Errors (cont’d.)
DropDownList1.Items.Add(reader.GetValue(0).ToString());
DropDownList2.Items.Add(reader.GetValue(1).ToString());
}
reader.Close();
connection.Close();