Microsoft Activex® Data Objects
Microsoft Activex® Data Objects
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
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
: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
16