Professional Documents
Culture Documents
Visual Programming: Muhammad Ahmad Jan
Visual Programming: Muhammad Ahmad Jan
INTRODUCTION TO ADO.NET
2
OVERVIEW OF THE ADO.NET
3
ARCHITECTURE OF THE
ADO.NET
The ADO.NET architecture can be divided into two logical pieces:
command execution and caching.
4
ARCHITECTURE OF THE
ADO.NET
5
COMPONENTS OF THE ADO.NET
• The classes provided by ADO.NET are core to develop a
professional data-driven application and they can be
divided into the following three major components:
• Data Provider
• DataSet
• DataTable
11/07/20 7
DATA PROVIDER
• Data Provider is nothing but a set of libraries that is used to
communicate with different data sources like Microsoft SQL server,
MS Access, Oracle etc.
• The Data Provider can also be called a data driver and it can be used
as a major component for your data-driven applications. The
functionalities of the Data Provider, as its name means, are to:
8
TYPES OF DATA PROVIDER
The Data Provider is physically composed of a binary library file
and this library is in the DLL file format. Sometimes this DLL file
depends on other DLL files, so in fact a Data Provider can be made
up of several DLL files. Based on the different kinds of databases,
Data Provider can have several versions and each version is matched
to each kind of database. The popular versions of the Data Provider
are:
• Open DataBase Connectivity (Odbc) Data Provider (ODBC.NET)
• Object Linking and Embeding DataBase (OleDb) Data Provider
(OLEDB.NET)
• SQL Server (Sql) Data Provider (SQL Server.NET)
• Oracle (Oracle) Data Provider (Oracle.NET)
9
TYPES OF DATA PROVIDER
• The different data providers are located at the different namespaces, and these
namespaces hold the various data classes that you must import into your code in
order to use those classes in your project.
• Following Table lists the most popular namespaces used by the different data
providers and used by the DataSet and the DataTable.
Table 3-1. Namespaces for different Data Providers, DataSet and DataTable
Namespaces Descriptions
System.Data Holds the DataSet and DataTable classes
Holds the class collection used to access an OLEDB data source
System.Data.OleDb
Holds the classes used to access an SQL Server 7.0 data source
System.Data.SqlClient
or later
Holds the class collection used to access an ODBC data source
System.Data.Odbc 10
Holds the classes used to access an Oracle data source
System.Data.OracleClient
THE ODBC DATA PROVIDER
The ODBC.NET supports the following Data Providers:
• SQL Server
• Microsoft ODBC for Oracle
• Microsoft Access Driver (*.mdb)
11
THE OLEDB DATA PROVIDER
• Microsoft Access
• SQL Server (7.0 or later)
• Oracle (9i or later)
12
THE SQL SERVER DATA PROVIDER
13
THE ORACLE DATA PROVIDER
14
DATA PROVIDER’S CLASSES
• Data Provider contains four classes: Connection, Command,
DataAdapter and DataReader. These four classes can be used to
perform the different functionalities to help you to:
• Set a connection between your project and the data source using the
Connection object
• Execute data queries to retrieve, manipulate and update data using the
Command object
• Move the data between your DataSet and your database using the
DataAdapter object
• Perform data queries from the database (read-only) using the
DataReader object
15
THE CONNECTION CLASS
The Connection object you want to use depends on the type of the
data source you selected. Data Provider provides four different
Connection classes and each one is matched to one different
database.
16
THE CONNECTION CLASS
Following Table lists these popular Connection classes used for the
different data sources:
17
THE CONNECTION CLASS
The connection string is a property of the Connection class and it
provides all necessary information to connect to your data source.
Regularly this connection string contains a quite few parameters to define
a connection, but only five of them are popularly utilized for most data-
driven applications:
• Provider
• Data Source
• Database
• User ID
• Password
18
THE PROVIDER PARAMETER
• Specifies the driver that the connection class uses to communicate with the
database.
• The most common drivers are:
• Microsoft.Jet.OLEDB.4.0 for Access
• SQLOLEDB for SQL Server
• MSDAORA for Oracle
19
THE DATA SOURCE PARAMETER
• is used to specify the server name of the computer on which the database
is running.
20
THE DATABASE PARAMETER
• Is self-explanatory and specifies the database name where your data resides
21
USER ID AND PASSWORD
PARAMETERS
• They are used to specify your database login credentials
• The User ID and Password defined in the database, not your Windows
login credentials.
22
THE CONNECTION CLASS
A typical data connection instance with a general connection string
can be expressed by the following codes:
Connection = New xxxConnection(“Provider = MyProvider;” & _
“Data Source = MyServer;” & _
“Database = MyDatabase;” & _
“User ID = MyUserID;” & _
“Password = MyPassWord;”)
The Data Source parameter indicates the name of the network computer on which
your SQL server or Oracle server is installed and running.
The Database parameter indicates your database name.
The User ID and Password parameters are used for the security issue for your
database. In most cases, the default Windows NT Security Authentication is utilized.
24
THE CONNECTION CLASS
Some typical Connection instances are listed below:
OLE DB Data Provider for Microsoft Access Database 2003
Connection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\database\CSE_DEPT.mdb;" & _
"User ID=MyUserID;" & _
"Password=MyPassWord;")
27
THE CLOSE() METHOD OF THE CONNECTION
CLASS
The Close() method is a partner of the Open() method and it is used to close a
connection between your database and your applications when you finished your
data operations to the data source. You should close any connection object you
connected to your data source after you finished the data access to that data
source, otherwise a possible error may be encountered when you try re-open that
connection in the next time as you run your project.
Unlike the Open() method, which is a key to your data access and operation to your
data source, the Close() method does not throw any exceptions when you try to
close a connection that has already been closed. So you do not need to use a
Try….Catch block to catch any error for this method.
28
THE DISPOSE() METHOD OF THE
CONNECTION CLASS
The Dispose() method of the Connection class is an overloaded method and
it is used to releases the resources used by the Connection object. You
need to call this method after the Close() method is executed to perform a
cleanup job to release all resources used by the Connection object during
your data access and operations to your data source.
After the Close() and Dispose() methods executed, you can release your
reference to the Connection instance by setting it to Nothing. A piece of
example code is shown in Figure 3-4.
Command class can be divided into the different categories and these
categories are based on the different Data Providers. For the popular Data
Providers, such as OLE DB, ODBC, SQL Server and Oracle, each one
has its own Command class. Each Command class is identified by the
different prefix such as OleDbCommand, OdbcCommand,
SqlCommand and OracleCommand. Although these different
Command objects belong to the different Data Providers, they have the
similar properties and methods, and they are equivalent in functionalities.
30
THE PROPERTIES OF THE COMMAND
CLASS
The Command class contains more than 10 properties, but only four
of them are used popularly in most applications:
• Connection property
• CommandType property
• CommandText property
• Parameters property
31
THE PROPERTIES OF THE COMMAND
CLASS
• Connection property holds a valid Connection object, and the Command object
can be executed to access the connected database based on this Connection object.
• Parameters property holds a collection of the Parameter objects. You must first
create and initialize a Parameter object before you can add that object to the
Parameters collection for a Command object.
32
THE CONSTRUCTOR OF THE COMMAND CLASS
33
THE METHODS OF THE COMMAND CLASS
Data Adapter
Command
Connection
• 35
Data Source
THE DATA READER CLASS
• DataReader is used to read the data from database
• it is a read and forward only connection oriented architecture during fetch the
data from database
• DataReader will fetch the data very fast when compared with dataset
• Generally we will use ExecuteReader object to bind data to datareader
• Example
• Conn.Open();
• SqlCommand cmd = new SqlCommand("Select UserName, First Name,LastName,
Location FROM Users", conn);
• SqlDataReader sdr = cmd.ExecuteReader();
• gvUserInfo.DataSource = sdr;
• gvUserInfo.DataBind();
• conn.Close();
• 36
DATASET AND DATATABLE
• The DataSet class can be considered as a table container and it can contain
multiple data tables. These data tables are only a mapping to those real data
tables in your database. But these data tables can also be used separately
without connecting to the DataSet. In this case, each data table can be
considered as a DataTable object.
• The DataSet and DataTable classes have no direct relationship with the Data
Provider class; therefore they are often called Data Provider-independent
components.
37
DATASET VS DATATABLE
• 38
DATASET VS DATATABLE
• 39
DATASET VS DATATABLE
• A DataSet holds one or more DataTables.
Example 1:
• DataTable dt = new DataTable();
• DataSet ds = new DataSet();
• ds.Tables.Add(dt);
•
Example 2:
• DataTable dt1 = new DataTable();
• DataTable dt2 = new DataTable();
• DataSet ds = new DataSet();
• ds.Tables.Add(dt1);
• ds.Tables.Add(dt2);
• 40
QUESTIONS ?
• 41