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

ADO.Net

Uploaded by

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

ADO.Net

Uploaded by

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

ADO.

NET
ADO.NET is a set of classes to interact with data sources such as
databases (data sources). ADO is the acronym for ActiveX Data Objects. It
allows us to interact with the different data sources like MySql,
Oracle ,SQL Server etc. (databases). It has classes and methods to
retrieve and manipulate data.
The following are a few of the .NET applications that use ADO.NET to
connect to a database, execute commands and retrieve data from the
database.
 ASP.NET Web Applications
 Console Applications
 Windows Applications.

Connection Architectures
There are the following two types of connection architectures:
1. Connected architecture: The application remains connected with
the database throughout the processing. Eg Connection, Command,
DataReader, DataAdapter etc.

2. Disconnected architecture: The application automatically


connects/disconnects during the processing. The application uses
temporary data on the application side called a DataSet.
Features of ADO.NET :
The following are the features of ADO.NET –
 Interoperability-
We know that XML documents are text-based formats. So, one can
edit and edit XML documents using standard text-editing tools.
ADO.NET uses XML in all data exchanges and for internal
representation of data.
 Maintainability –
ADO.NET is built around the idea of separation of data logic and user
interface. It means that we can create our application in independent
layers.
 Performance –
It uses disconnected data architecture which is easy to scale as it
reduces the load on the database. Everything is handled on the client-
side, so it improves performance.
 Scalability –
It means meeting the needs of the growing number of clients, which
degrading performance. As it uses disconnected data access,
applications do not retain database lock connections for a longer time.
Thus, it accommodates scalability by encouraging programmers to
conserve limited resources and allow users to access data
simultaneously.

ADO.NET Architecture:
In this diagram, we can see that there are various types of
applications (Web Application, Console Application, Windows Application
and so on) that use ADO.NET to connect to databases (SQL Server, Oracle,
OleDb, ODBC and so on).

Important Classes in ADO.NET


We can also observe various classes in the preceding diagram.
They are:
1. Connection Class
2. Command Class
3. DataReader Class
4. DataAdaptor Class
5. DataSet.Class
1. Connection Class
In ADO.NET, we use these connection classes to connect to the
database. These connection class manage transactions and connection
pooling..

2. Command Class
The Command class provides methods for storing and executing
SQL statements and Stored Procedures. The following are the various
commands that are executed by the Command Class. SELECT, INSERT,
UPDATE, and DELETE Database queries are used to obtain, add, and
remove data.
 ExecuteReader: Returns data to the client as rows. This would
typically be an SQL select statement or a Stored Procedure that
contains one or more select statements. This method returns a
DataReader object that can be used to fill a DataTable object or used
directly for printing reports and so forth.
 ExecuteNonQuery: Executes a command that changes the data in
the database, such as an update, delete, or insert statement, or a
Stored Procedure that contains one or more of these statements. This
method returns an integer that is the number of rows affected by the
query.
 ExecuteScalar: This method only returns a single value. This kind of
query returns a count of rows or a calculated value.
 ExecuteXMLReader: (SqlClient classes only) Obtains data from an
SQL Server 2000 database using an XML stream. Returns an XML
Reader object.
3. DataReader Class
The DataReader is used to retrieve data. It is used in conjunction
with the Command class to execute an SQL Select statement and then
access the returned rows.
4. DataAdapter Class
The DataAdapter is used to connect DataSets to databases. The
DataAdapter is most useful when using data-bound controls in Windows
Forms, but it can also be used to provide an easy way to manage the
connection between your application and the underlying database tables,
views and Stored Procedures.
5. DataSet Class
The DataSet is the heart of ADO.NET. The DataSet is essentially a
collection of DataTable objects. In turn each object contains a collection of
DataColumn and DataRow objects. The DataSet also contains a Relations
collection that can be used to define relations among Data Table Objects.

Steps to Database Connectivity


1. Create a Connection

2. Open the connection.

3. Create a Command class to pass the Query.

4. Execute the command.

5. Get the Result

6. Close the connection

Code for connecting to Oracle Database


If you want to connect to an Oracle database beginning use the
namespace System.Data.OralceClient.

C# code to add new record in the Oracle Database


using System.Data.OracleClient;
namespace DBConnect
{
class Connect
{
public static void Main(string[]args)
{
OracleConnection cn = new
OracleConnection(“uid=system;pwd=raj”);
cn.Open()

OracleCommand cmd = new OracleCommand("insert into employee

values(1,’Ram’,10000)”, cn);

cmd.ExecuteNonQuery();

cn.Close();

C# windows code to add new record in the Oracle Database

using System;
using System.Data;
using System.Windows.Forms;

namespace OracleConnection
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
OracleConnection con = new
OracleConnection("UId=System; Password=manager");
con.Open();
}
private void button1_Click(object sender, EventArgs e)
{
OracleCommand cmd=new OracleCommand("insert into employee
values('" + textBox1.Text + " ' , " +textBox2.Text + ")",con);
cmd.ExecuteNonQuery();
MessageBox.Show("Record Inserted Successfully…");
}
cn.Close();
}

You might also like