Professional Documents
Culture Documents
Dot Net - Chapter-4
Dot Net - Chapter-4
Lecture notes
Chapter-4
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
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.
2. Maintainability:
3. Programmability:
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.
- 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.
- ADO wasn't really designed for cross-language use. It was aimed primarily at VB
programmers.
- While ADO.Net supports multiple languages.
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.
(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
(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
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.
<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:
Insert code:
con.Open();
int i= cmd.ExecuteNonQuery();
if (i > 0)
{
Response.Write("Record Inserted Successfully!");
}
con.Close();
TextBox1.Text = "";
}
Update:
Update Code:
con.Open();
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
Response.Write("Record Update Successful !");
}
con.Close();
TextBox1.Text = "";
}
Delete:
Delete code:
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["co
nstr"].ConnectionString);
con.Open();
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
Response.Write("Record Deleted Successful !");
}
con.Close();
TextBox1.Text = "";
}
Show Records:
adap.Fill(ds, "emp");
GridView1.DataSource = ds.Tables["emp"];
GridView1.DataBind();
con.Close();
}
Dataset DataReader
2 Support multiple tables from different Support single table based on a single sql
database. query of database
10. What is a Typed and UnTyped Dataset in ADO.NET? Why do we use a Typed
DataSet?
Typed Dataset
UnTyped Dataset
- 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.
Note: See all these control in details with all its property and how to bind it with
Database.