0% found this document useful (0 votes)
35 views11 pages

ADO.NET Performance and Data Access

This chapter discusses data access in ADO.NET. It covers ADO.NET objects like connections, commands, data adapters and data readers. It also discusses creating and retrieving database connections using providers and connection strings. The chapter describes SqlDataSource controls and ASP.NET data-bound controls like GridView that can be used with SqlDataSource to display and manipulate data from a SQL Server database.

Uploaded by

om chavan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views11 pages

ADO.NET Performance and Data Access

This chapter discusses data access in ADO.NET. It covers ADO.NET objects like connections, commands, data adapters and data readers. It also discusses creating and retrieving database connections using providers and connection strings. The chapter describes SqlDataSource controls and ASP.NET data-bound controls like GridView that can be used with SqlDataSource to display and manipulate data from a SQL Server database.

Uploaded by

om chavan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter No. 5 Data Access with ADO.

NET
● Overview of [Link] Objects
● Create and retrieve Database Connections
● SqlDataSource Controls
● [Link] Data-Bound Controls
● GridView, Repeater, DataList, Details View, Form View
● Overview of [Link] Objects:
[Link] Architecture:-
The most important concept in [Link] architecture is “Data Provider”. Data Provider provides
access to data sources (SQL Server, Access, Oracle). In short, it provides an object to achieve
functionalities like opening and closing connection, retrieve data, and update data. In the below figure,
you can see the four main sections of a data provider:
● Connection.
● Command object (this is the object for using Stored Procedures).
● Data Adapter (this object acts as a bridge between the data store and the dataset).
● Data Reader (this object reads data from the data store in forward only mode).
● DataSet object represents disconnected and cached data. If you see the diagram, it is not in
direct connection with the data store (SQL Server, Oracle, etc.) rather it talks with the data
adapter, who is responsible for filling the dataset. The dataset can have one or more datatables
and relations.
● The DataView object is used to sort and filter data in the datatable.

[Link]:-
[Link] is the new database technology used in .NET platform. [Link] is the next step
of Microsoft ActiveX Data Objects (ADO). It shares much of the ADO functionality. The [Link]
covers the classes in the [Link] namespace. [Link] is a set of classes that expose the data
access services of the .NET Framework.
✔ ADO stands for ActiveX Data Objects
✔ [Link] is a database technology of .NET Framework used to connect application system
and database server.
✔ [Link] is a part of the .NET Framework
✔ [Link] consists of a set of classes used to handle data access
✔ [Link] uses XML to store and transfer data among applications, which is not only an
industry standard but also provide fast access of data for desktop and distributed applications.
✔ [Link] is scalable and interoperable
ADVANTAGES OF [Link]
1) Interoperability. [Link] uses XML as the format for transmitting data from a database to a
local memory.
2) Maintainability. When an increasing number of users work with an application, the increased use
can strain resources. By using n-tier applications, you can spread application logic across
additional tiers. [Link] architecture uses local in-memory caches to hold copies of data,
making it easy for additional tiers to trade information.
3) Programmability. The [Link] programming model uses strongly typed data. Strongly typed
data makes code more concise and easier to write because Microsoft Visual Studio .NET provides
statement completion.
4) Performance. [Link] helps you to avoid costly data type conversions because of its use of
strongly typed data.
5) Scalability. The [Link] programming model encourages programmers to conserve system
resources for applications that run over the Web. Because data is held locally in in memory caches,
there is no need to retain database locks or maintain active database connections for extended
periods.
CONNECTED VS DISCONNECTED: For much of the history of computers, the only environment
available was the connected environment. In many situations, people do not work entirely in a
connected or disconnected environment, but rather in an environment that combines the two
approaches.
Connected: A connected environment is one in which an application is constantly connected to a data
source. A connected architecture has following advantages:
a) A secure environment is easier to maintain.
b) Concurrency is easier to control.
c) Data is more likely to be current than in other scenarios.
A connected scenario has the following disadvantages:
a) It must have a constant network connection.
b) Scalability
Disconnected: A disconnected environment is one in which an application is not constantly connected
to a source of data. Users can take a subset of data with them on a disconnected computer, and then
merge changes back into the central data store.
A disconnected environment provides the following advantages:
a) You can work at any time that is convenient for you, and can connect to a data source at any
time to process requests.
b) Other users can use the connection.
c) A disconnected environment improves the scalability and performance.
A disconnected environment has the following disadvantages:
● Data is not always up to date.
● Change conflicts can occur and must be resolved.
.NET DATA PROVIDER
A .NET data provider is used for connecting to a database, executing commands, and retrieving
results. Those results are either processed directly, or placed in an [Link] DataSet in order to be
exposed to the user in an ad-hoc manner, combined with data from multiple sources, or remoted
between tiers. The .NET data provider is designed to be lightweight, creating a minimal layer between
the data source and your code, increasing performance without sacrificing functionality. The
[Link] object model includes the following data provider classes:
1. SQL Server .NET Data Provider
2. OLE DB .NET Data Provider
3. Oracle .NET Data Provider
4. ODBC .NET Data Provider
5. Other Native .NET Data Provider
1) SQL Server Data Provider: To use the SQL Server .NET Data Provider, you need to include the
[Link] namespace in your applications. Using this provider is more efficient than
using the OLE DB .NET Data Provider because it does not pass through an OLE DB or Open
Database Connectivity (ODBC) layer. It provides optimized access to SQL Server 2000 and SQL
Server 7.0 databases.
2) OLE DB Data Provider: To use the OLE DB .NET Data Provider, you need to include the
[Link] namespace in your applications. .NET Provides access to SQL Server
versions 6.5 and earlier. It also provides access to other databases, such as Oracle, Sybase,
DB2/400, and Microsoft Access.
3) Oracle .NET Data Provider: To use the Oracle Database, a native Oracle .NET data driver is the
best choice. To use the Oracle .NET Data Provider, you need to include the
[Link] namespace in your applications. Oracle itself also provides a .NET
data provider, referenced as [Link]. This is a separate download that you have
to get from Oracle. Whether you use a .NET data provider from the database vendor or just use the
one provided with the .NET framework is your choice.
4) ODBC .NET Data Provider: If you have a data source for which no native or OLE DB provider
is available, the ODBC .NET data provider is good alternative because most database provide an
ODBC interface. It is referenced with this using directive: [Link]
5) Other Native .NET Data Provider: If there is a native .NET data provider available specifically
for your database, then you may want to use that .NET data provider instead. Many other database
vendors and third-party companies provide native.

.NET data providers; the choice between using the native providers and using something
generic like the ODBC provider will depend on your circumstances. If you value portability over
performance, then go generic. If you want to get the best performance or make the best use of a
particular database„s features, go native.

● Create and Retrieve Database Connection:

Providers: An [Link] provider is a class that can communicate with a specific type of database or
data store, such as SQL Server, Oracle, MS-Access, etc. database. The providers included with the
.NET Framework are:

1. SqlServer:- The .NET Framework Data Provider for SQL Server in the [Link]
namespace. This provider is the default for the SqlDataSource control.
2. OleDb:- The .NET Framework Data Provider for OLE DB in the [Link]
namespace.
3. Odbc:- The .NET Framework Data Provider for ODBC in the [Link] namespace.

Connection Strings: A connection string provides the information that a provider needs to
communicate with a particular database. Depending on the provider, a connection string usually
supplies the server or location of the database server, the database name and authentication
information.

Creating Connection:
1) Open Visual Studio, select “Server Explorer” Connect to Database Add Connection. Window
will open.(like the following image.)

2) Select “Data Source” such as “Microsoft Access Database”


3) Click on “Browse” for selection of database name. Then click “Test Connection”.
4) Design web form using form controls.
5) Double click on the form and add some code
using [Link];
OledbConnection con;
OledbConnection con= new OledbCOnnection ("Provider =
[Link].4.0; Data Source=C:\[Link]");
Retrieve and display data
OLEDBCommand cmd=new OLEDBCOmman();
[Link]=con;
[Link]=[Link];
[Link]="Select * from emp";
OledbDataReader dr= new OledbDataReader();
dr=[Link]();
while ([Link]()==true)
{
// Do some coding here
}
● SqlDataSource Controls:
As you may guess, the SquDataSource control in [Link] 3.5 is designed to work with a
SQL Server database. It uses SQL Server .NET data provider internally to connect to the database.
SQL Server .NET data provider classes are defined in the [Link]
[Link] SqlDataSource data source control represents data in an SQL relational database to
data-bound controls. You can use the SqlDataSource control in conjunction with a data-bound control
to retrieve data from a relational database and to display, edit, and sort data on a Web page with little
or no code. SqlDataSource control inherited from DataSourceControl class, which provides common
functionality for all of these data source controls.
The SqlDataSource class provides a FilterExpression property that can be used to filter the
results of calling the SqlDataSource class' Select method.
To connect to a database, you must set the ConnectionString property to a valid connection
string. The SqlDataSource can support any SQL relational database that can be connected to using an
[Link] provider, such as the SqlClient, OleDb, Odbc, or OracleClient. The steps are as follows-
(1) Create a web site and add a SqlDataSourceControl on the web form.
(2) Click on the Configure Data Source option.
(3) Click on the New Connection button to establish connection with a database.
(4) Once the connection is set up, you may save it for further use. At the next step, you are asked
to configure the select statement:
(5) Select the columns and click next to complete the steps. These buttons allow you to specify the
insert, update, and delete commands of SQL. This way, you can manipulate the data.
(6) Add a GridView control on the form. Choose the data source and format the control using
AutoFormat option.
(7) After this the formatted GridView control displays the column headings, and the application is
ready to execute.
(8) Finally execute the application.
● [Link] Data bound controls:
Every [Link] web form control inherits the DataBind method from its parent Control class,
which gives it an inherent capability to bind data to at least one of its properties. This is known as
simple data binding or inline data binding.
Simple data binding involves attaching any collection (item collection) which implements the
IEnumerable interface, or the DataSet and DataTable classes to the DataSource property of the control.
On the other hand, some controls can bind records, lists, or columns of data into their structure
through a DataSource control. These controls derive from the BaseDataBoundControl class. This is
called declarative data binding.
The data source controls help the data-bound controls implement functionalities such as, sorting,
paging, and editing data collections.
The BaseDataBoundControl is an abstract class, which is inherited by two more abstract classes:
● DataBoundControl
● HierarchicalDataBoundControl
The abstract class DataBoundControl is again inherited by two more abstract classes:
● ListControl
● CompositeDataBoundControl
The controls capable of simple data binding are derived from the ListControl abstract class and these
controls are:
● BulletedList ● DropDownList
● CheckBoxList ● ListBox
● RadioButtonList
The controls capable of declarative data binding (a more complex data binding) are derived from the
abstract class CompositeDataBoundControl. These controls are:
● DetailsView ● GridView
● FormView ● RecordList
● GridView:
[Link] provides a number of tools for showing tabular data in a grid, including the
GridView control. It was introduced with [Link] 2.0. The GridView control is used to display the
values of a data source in a table. Each column represents a field where each row represents a record.
It can also display empty data. The GridView control provides many built-in capabilities that allow the
user to sort, update, delete, select and page through items in the control. The GridView control can be
bound to a data source control, in order to bind a data source control, set the DataSourceID property of
the GridView control to the ID value of the data source control. It's considered a replacement for the
DataGrid control from .NET 1.1. Therefore, it is also known as a super DataGrid. The GridView
control offers improvements such as the ability to define multiple primary key fields, improved user
interface customization using bound fields and templates and a new model for handling or canceling
events. Performance is slow compared to DataGrid and ListView.
The GridView control supports the following features:
● Improved data source binding capabilities
● Tabular rendering – displays data as a table
● Item as row
● Built-in sorting capability
● Built-in select, edit and delete capabilities
● Built-in paging capability
● Built-in row selection capability
● Multiple key fields
● Programmatic access to the GridView object model to dynamically set properties, handle
events and so on
● Richer design-time capabilities
● Control over Alternate item, Header, Footer, Colors, font, borders, and so on.
● Slow performance as compared to Repeater and DataList control
● Repeater:
The Repeater control was introduced with [Link] 1.0. The [Link] Repeater is a basic
container control that allows you to create custom lists from any data available to the page. It provides
a highly customized interface. It renders a read-only template; in other words, it supports only the
ItemTemplate to define custom binding. The Repeater control is a Data Bind Control, also known as
container controls. The Repeater control is used to display a repeated list of items that are bound to the
control. This control may be bound to a database table, an XML file, or another list of items. It has no
built-in layout or styles, so you must explicitly declare all layout, formatting and style tags within the
controls templates. You would require writing an explicit code to do paging using this control. The
Repeater repeats a layout of HTML you write, it has the least functionality of the rest of the three
controls.
The Repeater control supports the following features:
● List format
● Item as row
● Paging, Sorting and Grouping requires custom code writing
● DataList:
The DataList control was introduced with [Link] 1.0. DataList is the next step up from a
Repeater; except you have very little control over the HTML that the control renders. DataList allows
you to repeat columns horizontally or vertically. The DataList control renders data as a table and
enables you to display data records in various layouts, such as ordering them in columns or rows. You
can configure the DataList control to enable users to edit or delete a record in the table. We can use a
DataList control where we need a single-column list. The DataList control works like the Repeater
control, used to display the data in a repeating structure, such as a table. It displays data in a format
that you can define using a template and styles. However, it arranges the data defined in the template
within various HTML structures. This includes options for horizontal or vertical layout and it also
allows you to set how the data should be repeated, as flow or table layout. The DataList control does
not automatically use a data source control to edit data. Instead, it provides command events in which
you can write your own code for these scenarios. You can configure the DataList control where the
user can edit or delete a record in the table.
The DataList control supports the following features:
● Support for binding data source controls such as SqlDataSource, LinqDataSource and
ObjectDataSource
● Good for columns
● Item as cell
● Updatable

● DetailsView:
The DetailsView control was introduced with [Link] 2.0. The DetailsView control uses a
table-based layout where each field of the data record is displayed as a row in the control. Unlike the
GridView control, the DetailsView control displays one row from a data source at a time by rendering
an HTML table. The DetailsView supports both declarative and programmatic data binding. The
DetailsView control is often used in master-detail scenarios where the selected record in a master
control determines the record to display in the DetailsView control. It shows the details for the row in
a separate space. We can customize the appearance of the DetailsView control using its style
properties. Alternatively, we can also use Cascading Style Sheets (CSS) to provide styles to a
DetailsView control. A DetailsView control appears as a form of recording and is provided by
multiple records as well as insert, update and delete record functions.
The DetailsView control supports the following features:
● Tabular rendering
● Supports column layout, by default two columns at a time
● Optional support for paging and navigation.
● Built-in support for data grouping
● Built-in support for edit, insert and delete capabilities

● FormView: The FormView was introduced with [Link] 2.0. The FormView control renders a
single data item at a time from a data source, even if its data source exposes a multiple records data
item from a data source. It allows for a more flexible layout when displaying a single record. The
FormView control renders all fields of a single record in a single table row. In contrast, the
FormView control does not specify a pre-defined layout for displaying a record. Instead, you
create templates that contain controls to display individual fields from the record. The template
contains the formatting, controls and binding expressions used to lay out the form. When using
templates, we can place any control such as a dropdown list, checkbox and we can even place
tables and rich controls like a GridView and so on. A FormView is a databound control used to
insert, display, edit, update and delete data in [Link] that renders a single record at a time. A
FormView control is similar to a DetailView in [Link] but the only difference is that a
DetailsView has a built-in tabular rendering whereas a FormView requires a user-defined template
to insert, display, edit, update and delete data.
The FormView control supports the following features:
● Template driven
● Supports column layout
● Built-in support for paging and grouping
● Built-in support for insert, edit and delete capabilities

*-*-*

Common questions

Powered by AI

The SqlDataSource control in ASP.NET acts as a bridge between a data source and data-bound controls, facilitating data binding, retrieval, and operations like sorting easily with minimal code . It is flexible, supporting multiple data handlers including SQL Server, OleDb, and Odbc . In contrast, the GridView control specifically aims to display tabular data with enhanced features such as built-in sorting, paging, and row selection . It allows more customization with templates and supports dynamically dragging events . The DetailsView control, on the other hand, focuses on displaying and manipulating a single data record at a time, frequently used in master-detail scenarios . While it supports paging and CRUD operations, it displays records in a form-based layout rather than a table . Each control serves specific needs depending on the application's demands for layout, data interaction, and complexity .

The choice of .NET data provider significantly affects both performance and functionality. The SQL Server .NET Data Provider, using the System.Data.SqlClient namespace, offers optimized performance by avoiding OLE DB or ODBC layers, thus enhancing efficiency especially for SQL Server databases . Conversely, the OLE DB .NET Data Provider, which includes System.Data.OleDb, provides broader compatibility with databases like Oracle and Sybase but may not match the SQL Server provider's performance optimization . The Oracle .NET Data Provider is tailored for Oracle databases, enriching functionality by leveraging Oracle-specific features, though requiring additional downloads from Oracle . Hence, the selection should consider the trade-offs between performance and database-specific functionalities .

The GridView control in ASP.NET offers several advantages over the Repeater control for data representation. GridView provides built-in functionalities like sorting, paging, updating, and deleting data, which streamlines the development process and reduces the need for custom code . Additionally, it supports a tabular data presentation with built-in design and layout options, enabling developers to present records efficiently and with enhanced interactivity . In contrast, the Repeater control is mainly used for custom list presentations without built-in layout or style capabilities, requiring the developer to define the entire HTML structure and rendering logic manually . This makes the GridView more suitable for scenarios requiring features like complex data manipulation and rapid development with minimal custom coding .

Choosing between a native and a generic .NET data provider largely determines the trade-off between performance and portability. Native data providers, like those for SQL Server or Oracle, offer optimized performance and leverage unique database features . This can lead to more efficient operations and better exploitation of the database's capabilities. Conversely, generic providers, such as those using ODBC, prioritize portability over performance. They allow developers to write code applicable across various database systems without tailoring it to a specific SQL server, thus enhancing flexibility and simplifying maintenance across different databases . Therefore, if specific performance or feature optimizations are crucial, a native provider is preferred. Otherwise, for broader compatibility across multiple database types, a generic provider might be more beneficial .

In ADO.NET, a connected environment maintains a constant connection to a data source, making it easier to ensure data concurrency and security while keeping the data current. However, this approach requires a constant network connection, which can limit scalability . On the other hand, a disconnected environment allows applications to work independently of a continuous data connection, storing data locally and synchronizing changes back to the central database. This improves scalability and performance by reducing demand on resources and preventing the need for sustained database locks . The trade-off is that data might not always be up-to-date, and change conflicts could occur, requiring resolution .

The DataList control in ASP.NET is highly configurable in terms of layout, supporting both horizontal and vertical data presentations . This flexibility allows developers to specify how data items should be arranged, whether in rows or columns, and customize the appearance through templates and styles . Such configurations can significantly impact user interaction with data, as it not only improves aesthetic appeal but also enhances usability by organizing information more logically or attractively depending on the context. The ability to display data in various formats, such as lists or grids, caters to different viewing preferences and enhances engagement, which is crucial for applications that rely heavily on data visibility and interaction . This adaptability makes the DataList control particularly useful for applications requiring diverse data presentations without additional complex coding .

In ADO.NET, the disconnected environment enhances scalability and performance by reducing system resource usage. Unlike the connected environment which requires constant network connections and active database locks, the disconnected environment holds data in local memory caches. This design enables applications to function without maintaining continuous database connections, thus freeing up resources for other processes . It allows users to interact with data offline and synchronize updates back with the database when necessary, enhancing flexibility and reducing server load. Furthermore, this architecture facilitates better resource distribution across users and processes, leading to improved overall system performance and scalability. However, it requires mechanisms for handling data conflicts and ensuring data consistency during synchronization .

Managing conflict resolution in a disconnected environment in ADO.NET involves several strategies. When data is stored in local caches and changes are later synchronized, conflicts can occur if multiple users update the same data independently . These conflicts can be addressed by implementing mechanisms to detect, log, and handle them upon synchronization. One approach involves comparing original and current values at the database against the data being submitted, using a timestamp or version number to identify conflicts. Developers can then design logic to resolve conflicts automatically by deciding priorities (e.g., last writer wins) or manually, prompting user decisions on conflicting data entries . Additionally, tools like ADO.NET's built-in concurrency control models can be harnessed to facilitate conflict management, ensuring data consistency without significant loss of performance .

Using FormView and DetailsView controls in ASP.NET applications for master-detail scenarios has distinct implications on data interaction and presentation. The DetailsView control excels in displaying a single record at a time in a table format, enabling detailed inspection and manipulation of data such as inserts, updates, and deletions through built-in support . In contrast, the FormView offers more flexibility by allowing developers to design custom templates for displaying single records, which can include any ASP.NET control . Both controls allow for easy integration within master-detail architectures, where a GridView might present summary data, and selecting an item displays details in either FormView or DetailsView. The choice between them affects how data is presented, whether formatted using predefined tables in DetailsView or through customizable layouts in FormView, impacting user experience and development workload .

The Odbc .NET Data Provider in ADO.NET is designed to offer broad database portability by enabling access to any data source that supports the ODBC interface . This makes it an ideal choice for applications requiring compatibility with a wide range of database systems. However, this increased flexibility can come at the cost of performance, as the ODBC interface may introduce an additional layer of abstraction compared to native providers, potentially resulting in slower data access and execution times . Therefore, while the Odbc .NET Data Provider is beneficial for ensuring application portability across diverse databases, it may not provide the same level of optimization or speed as specialized native providers tailored to specific databases like SQL Server or Oracle .

You might also like