0% found this document useful (0 votes)
13 views16 pages

Microsoft Activex® Data Objects

Uploaded by

mohamed fikry
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)
13 views16 pages

Microsoft Activex® Data Objects

Uploaded by

mohamed fikry
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/ 16

What is ADO.NET?

ADO.NET is the new database technology


of the .NET (Dot Net) platform, and it builds
on Microsoft ActiveX® Data Objects
(ADO).
ADO.NET provides consistent access to
data sources such as Microsoft SQL Server,
as well as data sources exposed through
OLE DB and XML. Data-sharing consumer
applications can use ADO.NET to connect to
these data sources and retrieve, manipulate,
.and update data

ADO.NET is a set of classes that expose 1


ADO.NET architecture:

2
The ADO.NET Object Model:

3
Connected and
Disconnected Data
Access Architecture
Connected: DataReader :
Disconnected: DataAdapter,
DataSet

4
Introduction to the SQL
Connection : Connection:
A Class for a SQL
The SqlConnection class is defined in the
System.Data.SqlClient namespace.
using System.Data.SqlClient;

 SqlConnection Object :
To interact with a database, you must have a connection
to it.
SqlConnection using one of its two constructors :
o The default constructor: allows you to declare the
variable without specifying how the connection would
SqlConnection conn = new SqlConnection("Data Source=.;Initial
be carried.
;);Catalog=test;Integrated Security=true;")
5
o The second constructor: takes as argument a
string value
string strConnection = "Data Source=.;Initial Catalog=test;
Integrated Security=true;";
;SqlConnection conn = new SqlConnection(strConnection)

:The Authentication
The connection string of the SqlConnection class
includes an attribute called Trusted_Connection or
Integrated Security that can have a value of true,
false, yes, no, or SSPI with the SSPI having the
.same indication as true

Note: If you are establishing a trusted or simple


connection that doesn't need to be verified, you can
assign a value of true .or SSPI
6
 Opening and Closing a
Connection:
1. Opening a Connection:
After programmatically creating a connection
string, to apply it and actually establish the
connection, you must call the
.SqlConnection.Open()
Conn.Open(); method

2. Closing a Connection:
If you are working from a SqlConnection object,
to close a connection, you can call the
.SqlConnection.Close() method

Conn.Close();
7
 Commanding a Database:
An action you perform on the database server or
.on a database is called a command
The System.Data.SqlClient namespace provides
the SqlCommand class. To use it, you can declare a
variable of type SqlCommand using one of its
.constructors
 The Text to Command:
The SqlCommand class is equipped with Four
:constructors
The default constructor allows you to initiate a .1
command without specifying what action would be
taken. The action to perform is created as a string
SqlCommand cmd = new SqlCommand();
.string
statement
cmdstr = " SQL Query”;
cmd.CommandText = cmdstr;
8
 To provide SqlConnection object to the command,
you can assign it to the SqlCommand.Connection
property cmd.Connection =
;conn

The second constructor of the SqlCommand .2


class
SqlCommand cmd = new SqlCommand();
cmd.CommandText =" SQL Query";
cmd.Connection = conn;

The Third constructor : you can define the .3


command text when declaring the SqlCommand
.variable
SqlCommand cmd = new SqlCommand(" SQL Query");
cmd.Connection = conn; 9
The Fourth constructor: specify what connection .4
would carry the action at the same time you are
.creatingcmd
;SqlCommand the=command
new SqlCommand(" SQL Query", conn)

:Finally code is
SqlConnection conn = new SqlConnection("Data Source=.;
;Initial Catalog=test;Integrated Security=true;")
SqlCommand cmd = new SqlCommand(“SQL Query", conn);
Conn.Open();
Conn.Close();
 Command Execution:
The SqlCommand class is equipped with the
.ExecuteNonQuery() method
;SqlCommand cmd = new SqlCommand("SQL Code", conn)
conn.Open();
cmd.ExecuteNonQuery();
conn.Close(); 10
 Reading
Data:
To read data of a database, one of the objects
.you can use is called a data reader

11
 The SQL Data Reader:
To get a data reader, you can declare a variable of
type SqlDataReader. This class does not have a
.constructor
;SqlDataReader dr

To provide data to the reader, the SqlCommand


class is equipped with the ExecuteReader()
method.
;)(dr = cmd.ExecuteReader

 Using a SQL Data


Reader:
To access data that the reader acquired, you can
.call its Read() method
while (dr.Read())
} ;stm {
12
Complete Code: using
DataReader
SqlConnection conn = new SqlConnection("Data
Source=.; Initial Catalog=test;Integrated
;Security=true;")
SqlCommand cmd = new SqlCommand("SQL Query", conn);
SqlDataReader dr;
Conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
} ;stm {
Conn.Close();
SqlConnection conn = new SqlConnection("Data
Source=.; Initial Catalog=test;Integrated
;Security=true;")
SqlCommand cmd = new SqlCommand();
cmd.CommandText =“SQL Query";
cmd.Connection = conn;
Or SqlDataReader dr;
Conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
} ;stm {
Conn.Close();
13
 DataSet:
 The DataSet is similar to an array of disconnected
Recordset objects
It supports disconnected data access and operations
You no longer have to be connected to the database all
the time.
 DataSet use to support the creation and management
of a set of data
•DataTableCollection object containing null or multiple
DataTable objects (Columns, Rows, Constraints).
•DataRelationCollection object containing null or multiple
DataRelation objects which establish a parent/child relation
between two DataTable objects.
DataSet ds = new DataSet();

Note: A DataSet is a container; therefore, you have to


.fill it with data
14
DataAdapter:
DataAdapter object is like a bridge that links the database and a
Connection object with the ADO.NET-managed DataSet object
through its SELECT and action query Commands.
•SelectCommand
•UpdateCommand
•InsertCommand
•DeleteCommand

The DataAdapter includes three main methods:


•Fill (populates a DataSet with data).
•FillSchema (queries the database for schema information that is
necessary to update).
•Update (to change the database, DataAdapter calls the
DeleteCommand, the InsertCommand and the UpdateCommand
properties).
15
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);

To Show Data from DataBase into Data Grid View


SqlConnection conn = new SqlConnection("Data Source=.;
;Initial Catalog=test;Integrated Security=true;")
SqlCommand cmd = new SqlCommand();
cmd.CommandText =“ SELECT Query";
cmd.Connection = conn;
Conn.open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
Conn.close();

16

You might also like