You are on page 1of 16

ADO.

NET Data Provider

Agenda

Introduction to ADO.NET ADO.NET Architecture Objects in ADO.NET

Creating Command Object Executing a Command That Doesn't Return Rows. Executing a Command That Returns a Single Value. Parameterized Command Using DataReader Object Transactions Transaction Demo.

.NET Data Providers Connecting to Data Base (SQL Server) Properties and Methods of the SqlConnection Class Storing Connection Strings in the Configuration File Using Command Object

Introduction to ADO.NET

ADO.NET is a data-access subsystem in the Microsoft .NET Framework. ADO.NET was developed from ADO (ActiveX Data Objects). In the .NET Framework, the ADO.NET libraries appear under the System.Data namespace.

Supports both connected and disconnected data access.

ADO.NET Architecture

Objects in ADO.NET

Connected Objects

Disconnected Objects

Connection Transaction Command Parameter DataReader DataAdapter

Dataset DataTable DataRow DataColumn DataView Constraint

.NET Data Providers

The .NET data provider is the managed component of choice for database vendors to expose their data in the most effective way. Each database vendor should provide a .NET-compatible API that is callable from within managed applications. The Data Provider is responsible for providing and maintaining the connection to the database.

Connecting to Data Base (SQL Server)

Creating Connection Object

By creating instance of SqlConnection Class.


SqlConnection testConnection = new SqlConnection();

Connection String Property


SqlConnection testConnection= new SqlConnection( "Data Source=(local);Initial Catalog=Test;Integrated Security=SSPI");

(OR)
SqlConnection testConnection = new SqlConnection(); string testConnectionString = "Data Source=(local);Initial Catalog=Test;Integrated Security=SSPI"; testConnection.ConnectionString = testConnectionString;

Creating and Managing Connections

To create and manage connections, you need to:


Create a connection object. Create a command object. Open the connection object. Execute the SQL statement in the command object. Close the connection object.

Storing Connection Strings in the Configuration File

For each connection string in the application, add a new <add> element to the <appSettings> element in the configuration file, as follows:

The classes for accessing the configuration file are found in the System.Configuration namespace. Code below creates connection using connection string stored in configuration file
SqlConnection MyConnection; MyConnection = new SqlConnection(); MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings[constring].ConnectionString;

Using Command Object

The Command object is the heart of data processing with ADO.NET. The Command object wraps a SQL statement or a call to a stored procedure. Properties and Methods of the SqlCommand Class

Executing a Command That Doesn't Return Rows

Executing a Command That Returns a Single Value

Parameterized Command

Using DataReader Object

The SqlDataReader class defines a lightweight yet powerful object that is used to read information from a SQL database. SqlDataReader retrieves query results in a read-only, forward-only stream of information and will not let you perform updates.

Using DataReader Object

The following code snippet illustrates the typical loop you implement to read all the records of a query:

Thank You

You might also like