Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

Subject : Dot Net Technology (Department Elective)


Semester : 6th - Branch : CE - Subject Code : 2160711

Lecture notes
Chapter-4

1. Benefits of ADO.NET. Also Compare ADO.NET with Classic ADO.

ADO.NET ADO (ActiveX Data Objects)

1 CLR Based Library It is COM based Library

2 Disconnected architecture Ado requires active connection. Connection


oriented architecture

3 Data stored in XML Data stored in Binary form

4 XML integration possible XML integration not possible

5 Locking not available Locking feature is available

6 It is use dataset for data access It is use recordset object for data access

7 Dataset can have multiple tables In recordset we can have only one table

8 Firewall proof (Because of XML) Firewall might prevent execution of classic ado
and its execution will never be
interrupted

9 The performance in ADO.NET is Performance low


higher in comparison to ADO
because ADO uses COM

10 Use all .NET Providers Use OleDb & Odbc providers

Note: You have to explain each & every points in short.

Benefits of ADO.NET:

1. Interoperatibilty:
- ADO.NET stored data in XML format, so it can be used by any OS which has XML
components to read the xml documents.

[DOT NET TECHNOLOGY – 6th CE] Page 1


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

- Ado.net supports dual programming model.

2. Maintainability:

- ADO.NET supports two-tier & three-tier (levels) applications.


- So in client server two-tier Applications, if you changed in ADO.NET on sever side then
it will easily reflect into the client side.
- Due to multiple tiers you can manage application in efficient way.

3. Programmability:

- ADO.NET classes are grouped together according to their functionality.


- So it is easy to use appropriate class as well as methods of each class.
- Line of code reduced due to the concept of Namespace.

4. Scalability:
- ADO.NET supports disconnected architecture with database, so the limited databases
Resources can be used by several users, and it does not support Locking system of
database, so the limited databases cannot be locked.

5. Performance:
- ADO.NET provides fast execution of disconnected applications using DataSet over
Recordset (in classical ADO), because there can be multiple tables per DataSet while
only one table per Recordset.

Note: Also here you can add points from comparison

2. Explain Characteristics of ADO.Net

(1) Disconnected access

- This is most important characteristics of Ado.net.


- Previous database access technology, provided continuously connected data
access.
- An application creates connection to database and keeps the connection open for
the life of the application.
- However if application become more complex and database serve more and more
clients, connected data access is impractical or make a problem.

[DOT NET TECHNOLOGY – 6th CE] Page 2


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

- Open database connection is Expensive in terms of system resources. More


connection then more system resources required to handle it. So performance
decreased.
- Ado.net provides disconnected database access. In this model data access
connections are establish and left open only for a certain requirement.
- For example if you want to load data from database, just open connection, load
data into application & close connection.
- If you want to update the data then just open the connection, execute update
command and close again.
- In this case connection is open only for minimum require time. So ado.net saves
system resources so data access fast & increase the performance.
(2) Data store as XML (Dual programming model)

- Ado.net use XML (Extensive Markup Language) to store data while Ado data stores
in binary format.
- In reality, when we retrieve information from a database into an ADO.NET DataSet,
the data is stored in memory in an XML format.
- If we want we can modify values, remove rows and add new records using XML in
.Net
- So its provide Dual programming model.
(3) Managed code

- Like .Net class library component, Ado.net is the managed code environment which
means it can be used easily in .net application
- While ADO use COM to communicate and it is unmanaged code. So you have to
jump again & again from unmanaged code to managed code. That will reduce the
efficiency of program.
(4) Data providers

- ADO support OLEDB provider, Ado.net support exactly same. OLEDB jump from
managed to unmanaged.
- Ado.net support new managed provider like SqlServer data provider.

(5) Cross-Language Support

- ADO wasn't really designed for cross-language use. It was aimed primarily at VB
programmers.
- While ADO.Net supports multiple languages.

[DOT NET TECHNOLOGY – 6th CE] Page 3


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

3. Explain All ADO.Net Namespace

The ADO.NET hold mainly six namespaces in the .NET class library.

(1) System.Data:
- Contains fundamental classes with the core ADO.NET functionality.
- It contains classes used by all data providers.
- These include DataSet and DataRelation, which allow us to manipulate structured
relational data.
- These classes are totally independent of any specific type of database or the way
we use to connect to it.
(2) System.Data.Common:
- All Data provider share these classes.
- These classes are not used directly in the code. Instead, they are used by other
classes.
- Example DbConnection, DbCommand, DbDataAdapter etc...
(3) System.Data.OleDb
- This namespace is used to use OleDbDataSource using OleDbDataProvider.
- If you want to make database connectivity with access database import this
namespace.
- It contains the classes like OleDbCommand and OleDbConnection.
(4) System.Data.SqlClient
- This namespace is used to use SqlDataSource
- If you want to make database connectivity with Microsoft SQL Server database
import this namespace.
- It contains the classes like SqlCommand and SqlConnection.

(5) System.Data.Odbc
- This namespace is used to use OdbcDataSource using OdbcDataProvider.
- If you want to make database connectivity with Oracle database import this
namespace.
- It contains the classes like OdbcCommand and OdbcConnection.
(6) System.Data.SqlTypes
- This Namespace is used to represents specific DataTypes for the SqlServer
Database.

[DOT NET TECHNOLOGY – 6th CE] Page 4


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

4. Explain Different ADO.NET Objects.


(1) Connection Object
- Connection object allow you to establish a connection.
- Connection object is used to make the link between data source and application.
- Connection object discover the data source and login to it properly.
- This information provided by ConnectionString
- Connection object for different providers: SqlConnection, OleDbConnection,
OdbcConnection
- Properties of Connection object.

DataSource Returns the Name of the instance of database


Database Returns the Name of the Database to use after connection
open.
State It returns the current status of the database whether it is
Broken, Closed, Connecting, Executing, Fetching and Open.
ConnectionString It is used to connect the database with database path string.
It provides full information regarding database like name of
database, name of database server, username password etc…

(2) Command Object


- Command object uses the connection object to execute the Query.
- Command object is used to write the query and then execute it.
- The query can be in the form of text, stored procedure or direct table format.
- Command object provides a number of Execute methods to execute the query.
- Properties of Command object

CommandText Retrieve either query statement or stored procedure


CommandType It is used to specify the type of query whether text, stored
procedure or direct table.
Connection Set Connection object which is used by this Command object

CommandTimeout - Returns no. of seconds wait while attempting to execute a


command.
- Command is aborted after Timeout. And Default 30 seconds

- Command Object’s Execute methods

ExecuteNonQuery It is used to execute insert, update & delete queries and it


returns the number of rows affected.
ExecuteReader It executes the command and it is read-only, forward-only

[DOT NET TECHNOLOGY – 6th CE] Page 5


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

cursor that allow you to retrieve Rows from DataSource


ExecuteScalar It execute the command and return the first column of first row

(3) DataReader
- DataReader object is a simple forward only and Read only cursor.
- It requires live connection with the DataSource.
- This object cannot be directly instantiated instead you must call the ExecuteReader
method of the command object to get valid DataReader object.
- Be sure to close the Connection when you use DataReader otherwise Connection
stays alive until it is explicitly closed.
- You can close Connection object by calling the Close method of Connection object.
- DataReader is a stream of data that is returned from Database query.
- DataReader reads one row at a time from the Database and can only move forward
one record at a time.
- As the DataReader reads the rows from the Database, the value of the columns in
each row can be read and evaluated but cannot be edited.
- As DataReader is forward only, we cannot fetch data randomly.

(4) DataAdapter
- DataAdapter is the Bridge between Disconnected Dataset and DataSource
- It provides two way Data transfer mechanism.
- From Dataset to Database and From Database to Dataset
- It is capable of executing select statement, retrieve data from DataSource and
transferring this result set into DataTable/Dataset
- It is capable of executing Insert, Update and Delete statement.
- Properties of DataAdapter

SelectCommand This command is automatically executed to fill a DataTable with


the result data.
InsertCommand This command is automatically executed to insert a new record
into the database.
UpdateCommand This command is automatically executed to update an existing
record in the databse.
DeleteCommand This command is automatically executed to delete an existing
record in the databse.

- It provides also Fill method.


- Calling Fill method automatically executed the command provided in select
command property, receives the result set & copied it to DataTable/Dataset.
-

[DOT NET TECHNOLOGY – 6th CE] Page 6


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

(5) DataSet
- Dataset is the most commonly used part of Ado.net
- Dataset is a Disconnected and used to representation of database.
- It is the local copy of relevant database
- We can updating the record in Dataset without updating original database.
- Data can be loaded into Dataset from any valid DataSource such as SqlServer DB,
MSAccess DB or from XML file.
- Dataset first introducing during .Net version 1.0
- The current version of Ado.net retains all the previous features as well as provides
some new features.
- In DataSet, There can be multiple DataTables and DataRelations.
- It contains the collection of data retrieved from the Datasource (Database).
- And the DataAdapter object is used to transfer the data between DataSet and
Database.

DataTable

- DataTable object represents Tables.


- You can add DataTable in Dataset object.
- DataTable contains collection of DataRow which contains all the Data in the Table.
- It contains also collection of DataColumns which contains information about each
column in Table and collection of constraint which specifies additional column
metadata such as primary key.

DataRelation

- Data Relation object allows you to create Join (Association) between Rows in one
Table and Rows in another Table.
- It defines relationship between multiple tables.
- For example consider a dataset that has two related tables : employee and projects
- Each employee represented only once and identified by unique employeeID field.
- Now in project table, an employee in charge of project is identified by employeeID
field. But can appear more than once if that employee is in charge of multiple
projects.
- This is example of one to many relationships.
- You can use DataRelation object to define this relationship.
- DataSet is a collection of DataTables and as shown in below figure, DataTable is a
collection of Rows Collection, Column Collection, Constraints Collection, and Child
Relation & Parent Relation Collections.

[DOT NET TECHNOLOGY – 6th CE] Page 7


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

[Fig. Basic Structure of DataSet]

5. Explain Structure /Architecture of ADO.Net. or Explain managed Data


Provider in .Net

- Draw this figure and Explain here all Ado.net object


- Explain Connection object, Command object, DataReader, Dataset, Data Table, Data
Relation, DataAdapter

[DOT NET TECHNOLOGY – 6th CE] Page 8


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

6. Explain Command object’s Execution Methods.

- Refer Command object from Ado.net object

7. What are DataReader, DataAdapter and Dataset with Example? When to


use DataReader or DataSet?

- Refer Ado.net objects for DataReader, DataAdapter and Dataset


- DataReader and dataset both are used for select statement that means a statement
which returns result set.
- If you want to retain the result set or want to use this result set further, you must
use DataAdapter object to fill a Dataset or DataTable.
- If you do not want to retain the result set or your aim is to simply displaying that
result set then, simply use DataReader object with appropriate command object’s
method.
Example : How to retrieve record from database using DataReader

SqlConnection con = new


SqlConnection(ConfigurationManager.ConnectionStrings["co
nstr"].ConnectionString);
SqlCommand cmd = new SqlCommand("select * from emp", con);
con.Open();
SqlDataReader r;
r = cmd.ExecuteReader();
GridView2.DataSource = r;
GridView2.DataBind();
con.Close();

Example : How to retrieve record from database using Dataset

SqlConnection con = new


SqlConnection(ConfigurationManager.ConnectionStrings["const
r"].ConnectionString);
SqlCommand cmd = new SqlCommand("select * from emp",con);
DataSet ds = new DataSet();
SqlDataAdapter adap = new SqlDataAdapter(cmd);
adap.Fill(ds,"emp");
GridView1.DataSource=ds.Tables["emp"];
GridView1.DataBind();

- Store connectionString in web.config file to run this code.

[DOT NET TECHNOLOGY – 6th CE] Page 9


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

Where to find connectionString information ?


- Right click on databse and go in property window, you can find connectionString
property, simply copy it and paste in web.config file as shown below.

<connectionStrings>
<add name="constr" connectionString="Data Source=
KITRC\SQLEXPRESS; Initial Catalog=test; Integrated
Security=True;"/>
</connectionStrings>

- If you don’t want to write connectionString manually in web.config file, just open
database in solution explorer, Open tables and drag any one Table in windows/web
forms.
- So automatically one gridview and datasource will add, delete it.
- Now open web.config file, you can see ConnectionString already added. No need to
add manually.
- DataReader needs live connection & works as a forward-only, read-only cursor.
- DataAdapter is a bridge between Dataset and Database.
- Dataset is the local copy of relevant database and you can Load multiple Tables in
single Dataset.

8. In SQLSERVER a table called employee contains the basic details about Employee such as
id, name and city. Develop Ado.Net Program using C#.Net which Insert, update and
delete in Web Application. Also display records in gridview.[here id primary key and auto
increment] – INSERT, UPDATE, DELETE and RETRIEVE in DATABASE.

Insert:

[DOT NET TECHNOLOGY – 6th CE] Page 10


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

Insert code:

protected void Button1_Click(object sender, EventArgs e)


{
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings
["constr"].ConnectionString);

SqlCommand cmd=new SqlCommand("insert into employee values


('"+ TextBox1.Text +"','"+ DropDownList1.SelectedItem.Text
+"')",con);

con.Open();
int i= cmd.ExecuteNonQuery();
if (i > 0)
{
Response.Write("Record Inserted Successfully!");
}

con.Close();

TextBox1.Text = "";
}

Update:

[DOT NET TECHNOLOGY – 6th CE] Page 11


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

Update Code:

protected void Button1_Click(object sender, EventArgs e)


{
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings
["constr"].ConnectionString);

SqlCommand cmd = new SqlCommand("update employee set name='"


+ TextBox1.Text + "', city='" +
DropDownList2.SelectedItem.Text + "' where id='"+
DropDownList1.SelectedValue +"'", con);

con.Open();
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
Response.Write("Record Update Successful !");
}

con.Close();
TextBox1.Text = "";
}

Delete:

[DOT NET TECHNOLOGY – 6th CE] Page 12


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

Delete code:
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["co
nstr"].ConnectionString);

SqlCommand cmd = new SqlCommand("delete from employee


where id='" + DropDownList1.SelectedValue + "'", con);

con.Open();
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
Response.Write("Record Deleted Successful !");
}

con.Close();
TextBox1.Text = "";
}

Show Records:

[DOT NET TECHNOLOGY – 6th CE] Page 13


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

Show Records Code:

protected void Button4_Click(object sender, EventArgs e)


{
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["co
nstr"].ConnectionString);
con.Open();

SqlCommand cmd = new SqlCommand("select * from


employee", con);

DataSet ds = new DataSet();


SqlDataAdapter adap = new SqlDataAdapter(cmd);

adap.Fill(ds, "emp");

GridView1.DataSource = ds.Tables["emp"];
GridView1.DataBind();
con.Close();
}

9. Difference between Dataset and DataReader

Dataset DataReader

1 Read/write access Read access only

2 Support multiple tables from different Support single table based on a single sql
database. query of database

3 Disconnected mode Connected mode

4 Bind to multiple controls Bind to single control

5 Forward & Backward data Forward only data

6 Slower access to data Faster access to data

7 Overhead Light weight object

[DOT NET TECHNOLOGY – 6th CE] Page 14


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

10. What is a Typed and UnTyped Dataset in ADO.NET? Why do we use a Typed
DataSet?

Typed Dataset

- A typed DataSet is not a built-in member of the .NET Framework. It is a generated


class that inherits directly from the DataSet class, and allows properties and
methods to be customized from an XML schema that you specify.
- This class also contains other classes for DataTable and DataRow objects.
How to create a Typed DataSet?
- Write click on Solution Explorer --> Add New Item --> Select DataSet
- This adds a new XSD to the project.
- The schema created may be viewed as an XML. When this xsd file is compiled, two
files are created by Visual Studio.
- The first file that contains the .vb or .cs extension contains the information about
the proxy class. This class contains methods & properties that are required to
access the database data.
- The second file has an extension xss and this contains information about the layout
of the XSD.
- The typed dataset is binded with the database tables(s) at design time and you
have all the schema information at design time in your code.
- For example you can access the tables with the actual names, columns with the
actual names. It is added as .xsd file in your application.
- It provides additional methods, properties and events and thus it makes it easier to
use.
- They have .xsd file (Xml Schema definition) associated with them and do error
checking regarding their schema at design time using the .xsd definitions.
- Using Typed Dataset We will get advantage of intelliSense in VS. NET.
- You can work it with simply by drag and drop table into dataset.
- Performance is slower in case of strongly typed dataset
- In complex environment, strongly typed dataset's are difficult to administer.

UnTyped Dataset

- Note that an UnTyped DataSet does not have any schema.


- Untyped dataset is an object of class System.Data.DataSet. It is binded with the
tables at runtime. You are not aware of the schema of the dataset at design time.
- It is not as easy to use as strongly typed dataset.

[DOT NET TECHNOLOGY – 6th CE] Page 15


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

- Drag and drop is not possible here you have to write code to work with it.

- They do not do error checking at the design time as they are filled at run time when
the code executes.
- We cannot get an advantage of intelliSense.
- Performance is faster in case of untyped dataset.
- Untyped datasets are easy to administer.

11. Explain Data Binding Controls:

There are two types of Data Binding Controls:

1. List Data Bind Control


- DropDownList
- CheckBoxList
- RadioButtonList
- BulletedList
2. Composite Data Bind Control
- GridView
- FormView
- DetailsView
- DataList
- Repeater

Note: See all these control in details with all its property and how to bind it with
Database.

[DOT NET TECHNOLOGY – 6th CE] Page 16

You might also like