aspnet6
aspnet6
SURENDRANAGAR
CLASS:- BCA/BSCIT SEMESTER -5
SUBJECT:- Asp.net
Prepared By:- Bhartiba parmar
Chapter-4
ADO.NET
• Contents
Introduction of ADO.NET
Component of ADO.NET
Understanding Data Provider
ADO.NET Architecture
GridView Control
Reapter Control
Introduction of ADO.NET
• Most application require some form of data access.
• ADO.NET is a part of the Microsoft .NET framework.
• The full form of ADO.net is Activex Data Object.
• ADO.NET has the ability to separate data access mechanisms,data manipulation
mechanisms and data connectivity mechanisms.
• ADO.NET is a set of classes that allow applications to read and write information
in databases.
• ADO.NET can be used by any .NET language.
• We need to add System.Data namespace for work with ADO.NET.
.NET Framework Data Provider for OLE DB It is used to connect with OLE DB. It requires the System.Data.OleDb namespace.
.NET Framework Data Provider for ODBC It is used to connect to data sources by using ODBC. It requires the System.Data.Odbc namespace.
.NET Framework Data Provider for Oracle It is used for Oracle data sources. It uses the System.Data.OracleClient namespace.
.NET Framework Data Provider for SQL Server Compact It provides data access for Microsoft SQL Server Compact 4.0. It requires
4.0. the System.Data.SqlServerCe namespace.
ADO.NET Architecture
Connected Architecture
• Connected architecture simple means you are connected with the
database through out your operation.
• It is faster then disconnected architecture.
• This creates more traffic to the database end.
Client Web Command Connection
Page object object database
Dataset or
datatable
object
• Disonnected architecture classes:-
DataAdapter
DataSet
DataTable
DataColumn
DataRow
Difference between Connected and disconnected architecture
Connected Disconnected
It is connection oriented. It is dis_connection oriented.
Connected methods gives faster performance Disconnected get low in speed and performance.
Datareader DataSet
Connected can hold the data of single table Disconnected can hold data of multiple tables
Connected you need to use a read only forward only In Disconnected you cannot use.
data reader
Data Reader can’t persist the data Data Set can persist the data
It is Read only, we can’t update the data. We can update the data.
Connection Class:-
• It is used to establish an open connection to the SQL Server database.
• The connection object contains all of the information required to open a
connection to the database.
• Syntax:
Sqlconnection connectionobject= New Sqlconnection(“ConnectionString”);
• Properties of Connection Object
Property Description
ConnectionString Sets or returns the details used to create a connection to a data source
ConnectionTimeout Sets or returns the number of seconds to wait for a connection to open
DefaultDatabase Sets or returns the default database name
Provider Sets or returns the provider name
State Returns a value describing if the connection is open or closed
Version Returns the ADO version number
Most important method of DataAdapter. This method is used to save the changes back from DataSet / DataTable to
actual database table.
Update Before using Update() method you need to use SqlCommandBuilder class so that it can generate related INSERT,
UPDATE, DELETE, ALTER, etc. command automatically
Example:-
protected void btnDisplay_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=E:\student.mdf;Integrated
Security=True;Connect Timeout=30;User Instance=True");
con.Open();
string sql = "select * from stud";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataTable dt=new DataTable();
da.Fill(dt);
gridDisplay.DataSource = dt;
gridDisplay.DataBind();
con.Close();
Display
}
DataSet Class
• This is used if you want to load some group of tables into local
memory. You can also use DataSet to load single table data. DataSet is
collection of tables.
Properties of DataSet Object
Property Description
DataSetName Gets or sets the name of the current DataSet.
HasErrors Gets a value indicating whether there are errors in any of the DataTable objects within this DataSet.
IsInitialized Gets a value that indicates whether the DataSet is initialized.
Tables Gets the collection of tables contained in the DataSet.
Allows you to select group of data from table. It has different methods to select the data. This is
Select
overloaded method which allows you to filter the data in variety of ways.
WriteXml() Writes the current contents of the DataTable as XML using the specified Stream or File.
DataRow Class :
This is used incase you want to add one new row into DataSet or
DataTable. You can also use DataRow to remove rows from DataSet or
DataTable.
DataColumn Class :
This is used incase you want to modify any column or you want to add
a new column to DataSet or DataTable.
GridView Control
• The GridView is the most powerful control.
• It displayes data in tabular format.
Properties of GridView
Description
AllowPaging It is used to set pagging if table data is too long.
If you set AllowPaging to true pagesize works.It breaks the pages as per size specified in PageSize
PageSize
Property.
AllowSorting It is used to sort data in gridview.
AutoGeneratedColumns It is used to automatically generate columns which are in bounded table
AutoGenerateDeleteButton Displays delete link button with records.
AutoGenerateEditButton Displays edit link button with records.
AutoGenerateSelectButton Displays select link button with records.
Caption Sets caption for GridView.
GridLines Allows you to display gridlines withing GridView.
Methods of GridView
Method Description
DataBind Allows you to bind the data with specified data source.
DeleteRow It is used to delete specified row from gridview.
UpdateRow It is used to update specified row in gridview.
Sorts the data in GridView according to two parameter.1)SortExpression
Sort
2)SortDirection
Repeater Control.
• The Repeater control is a simple container control that binds to a list
of items.
• It walks through the bound items and produces graphical elements
according to a basic rendering algorithm and the HTML templates you
supply.
• The Repeater control supports from one through five templates.
These templates, which form a tabular structure, are described in
Table.
• Repeter Control Properties
Property Description
DataSource Specify dataset or datatable as a datasource.
HeaderTemplate Specify HeaderTemplate information.
ItemTemplate Specify ItemTemplate information.
AlternatingItemTemplate Specify AlternatingItemTemplate information.
FooterTemplate Specify FooterTemplate information.
SeperatorTemplate Specify SeperatorTemplate information.
Items Specify each item inside repeater control.
Templates Supported by the Repeater Control
Template Description
HeaderTemplate This template is used to specify headings of repeater control. This
template is rendered only once before displaying any row.
ItemTemplate Determines the output format of each row in the data source. This
template is called for each item in the list and can contain data binding
expressions.
2)Complex DataBinding
When you connected to any single column of a table or with entire table it’s known as complex DataBinding
Controls like DropDownList,CheckBox List,RadioButtonList are known as Complex Data Bind Controls which can
be bounded to a particular column. They can show only one column.
Controls like Data Grid , Grid View or Repeater are known as Complex Data Bind Controls which can be
bounded to entire table. They can show entire table.
Example:-
DataGrid1.DataSource=dt;
DataGrid1.DataBind();