0% found this document useful (0 votes)
1 views

C#-Unit5

ADO.NET is a framework for interacting with data sources like databases and XML files, used in various .NET applications. It includes important classes such as Connection, Command, Data Reader, Data Adapter, and DataSet, which facilitate data retrieval and manipulation. The .NET Framework supports multiple programming languages and provides a runtime environment, with key components including the Common Language Runtime (CLR) and the .NET Framework Class Library (FCL).

Uploaded by

dummy10802
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

C#-Unit5

ADO.NET is a framework for interacting with data sources like databases and XML files, used in various .NET applications. It includes important classes such as Connection, Command, Data Reader, Data Adapter, and DataSet, which facilitate data retrieval and manipulation. The .NET Framework supports multiple programming languages and provides a runtime environment, with key components including the Common Language Runtime (CLR) and the .NET Framework Class Library (FCL).

Uploaded by

dummy10802
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

UNIT V

ADO.NET

ADO.NET is a set of classes (a framework) to interact with data sources such as databases and
XML files.ADO is the acronym for ActiveX Data Objects .It allows us
toconnecttounderlyingdataordatabases.Ithasclassesandmethodstoretrieve and manipulate data.

The following are a few of the .NET applications that use ADO.NET to connect to a database,
execute commands and retrieve data from the database.

● ASP.NET Web Applications


● Console Applications
● Windows Applications.
Various Connection Architectures

There are the following two types of connection architectures:

1. Connected architecture: the application remains connected with the database


throughout the processing.

2. Disconnected architecture: the application automatically connects/disconnects during


the processing. The application uses temporary data on the application side called a
DataSet.
ImportantClassesinADO.NET

We can also observe various classes in the preceding diagram. They are:

● Connection Class
● Command Class
● Data Reader Class
● Data Adaptor Class
● DataSet. Class

1. Connection Class
In ADO.NET, we use these connection classes to connect to the database. These connection
classes also manage transactions and connection pooling. To learn more about connection
classes, start here: Connection in ADO.NET.

2. Command Class
The Command class provides methods for storing and executing SQL statements and Stored
Procedures. The following are the various commands that are executed by the Command
Class.
Execute Reader: Returns data to the client as rows. This would typically be an SQL select
statement or a Stored Procedure that contains one or more select statements. This method returns
a Data Reader object that can be used to fill a DataTable object or used directly for printing
reports and so forth.

Execute Non-Query: Executes a command that changes the data in the database, such as an
update, delete, or insert statement, or a Stored Procedure that contains one or more of these
statements. This method returns an integer that is the number of rows affected by the query.

Execute Scalar: This method only returns a single value. This kind of query returns a count of
rows or a calculated value.

Execute XML Reader: (Sql Client classes only) Obtains data from an SQL Server 2000
database using an XML stream. Returns an XML Reader object.

3. Data Reader Class


The Data Reader is used to retrieve data. It is used in conjunction with the Command class to
execute an SQL Select statement and then access the returned rows. Learn more here: Data
Reader in C#.

4. Data Adapter Class


The Data Adapter is used to connect Data Sets to databases. The Data Adapter is most useful
when using data-bound controls in Windows Forms, but it can also be used to provide an easy
way to manage the connection between your application and the underlying database tables,
views and Stored Procedures. Learn more here: Data Adapter in ADO.NET.

5. Data Set Class


The Data Set is the heart of ADO.NET. The Data Set is essentially a collection of Data Table
objects. In turn each object contains a collection of Data Column and Data Row objects. The
Data Set also contains a Relations collection that can be used to define relations among Data
Table Objects.

The .NET Framework is a software development framework developed by Microsoft that provides a
runtime environment and a set of libraries and tools for building and running applications on Windows
operating systems. The .NET framework is primarily used on Windows, while .NET Core is cross-
platform. The framework supports multiple programming languages, such as C#, F#, and VB.NET, and
supports a range of application types, including desktop, web, mobile, cloud, and gaming applications.

Key Components of the .NET Framework


The .NET Framework’s basic architecture consists of two key elements:
1. Common Language Runtime (CLR): The Common Language Runtime (CLR) is the core runtime
engine of the .NET Framework, responsible for managing the execution of code written in any of
the .NET-supported languages, such as C#, F#, and VB.NET. It provides various services, including:
 Memory Management: Automatic memory allocation and garbage collection.
 Code Safety: Ensures type safety and handles security through Code Access Security (CAS).
 Execution Management: Converts Intermediate Language (IL) code into machine code via the Just-In-
Time (JIT) compiler.

The CLR enables Cross-Language Interoperability, which allows components written in different
languages to work together seamlessly.

2. .NET Framework Class Library (FCL): The .NET Framework Class Library (FCL) is a vast
collection of pre-built functions and classes available to developers. The FCL provides a range of
namespaces that contain classes for tasks like:
 File I/O
 Networking
 Database access
 Graphical User Interface (GUI) design
Additionally, it includes tools such as the Visual Studio IDE, which simplifies application development,
debugging, and testing.
3. Other Key Components:
 Common Type System (CTS): It ensures that types are consistent across different languages within
the .NET ecosystem.
 Common Intermediate Language (CIL): This is a low-level language that is compiled from source
code and later converted into machine code by the JIT compiler.
 Assemblies: The bundles of code that are versioned and deployed together, providing the essential
building blocks of a .NET application.

Basic Architecture and Component Stack of .NET Framework


Managed Code & Garbage Collection

✔ Managed Code: Code executed under CLR supervision.


✔ Garbage Collection (GC): Automatically removes unused objects, preventing memory leaks.

Just-In-Time (JIT) Compiler

Converts Intermediate Language (IL or MSIL) into machine code at runtime, improving performance.

ASP . NET controls


In ASP.NET, data source controls are server controls that retrieve and manage data from various
sources, allowing data-bound controls to display, edit, and manipulate data with minimal code. Key
data source controls include SqlDataSource, ObjectDataSource, and EntityDataSource.

Data source controls act as intermediaries between data-bound controls (like GridView, FormView,
DetailsView) and the data source (database, business object, XML file, etc.).
 Functionality:

They handle tasks like:

 Connecting to data sources

 Retrieving data

 Sorting, paging, and filtering data

 Supporting data modification (insert, update, delete)

 Caching data for performance

Common Data Source Controls:

 SqlDataSource:

Connects to and retrieves data from SQL databases.

 Supports various SQL database types (e.g., SQL Server, Oracle, OLE DB, ODBC).

 Uses ADO.NET for database interaction.


Example:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDB %>"
SelectCommand="SELECT * FROM Employees">

</asp:SqlDataSource>

ObjectDataSource:

Connects to and retrieves data from business objects or methods that return data.

 Useful for implementing multi-tier architectures.

 Can bind to methods that return DataSet, DataTable, or IEnumerable objects.

Example:
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
TypeName="MyNamespace.EmployeeService"
SelectMethod="GetEmployees">
</asp:ObjectDataSource>

EntityDataSource:

Connects to and retrieves data from Entity Framework entities.

 Simplifies data access using object-relational mapping.

AccessDataSource:

Connects to and retrieves data from Microsoft Access databases.

 Uses SQL queries to perform data retrieval.

LinqDataSource:
Enables use of LINQ (Language Integrated Query) to retrieve and modify data from a data object.

Steps to Design Web Applications


Step 1: Create an ASP.NET Web Application

1. Open Visual Studio.


2. Click File → New → Project.
3. Select ASP.NET Web Application.
4. Choose Web Forms and click Create.

Step 2: Configure the Database

1. Set up a database (SQL Server or another DBMS).


2. Create a table (e.g., Employees).
3. Insert sample data.

Example: SQL Table


sql
Copy code
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name NVARCHAR(100),
Position NVARCHAR(100),
Salary DECIMAL(10,2)
);
INSERT INTO Employees VALUES (1, 'John Doe', 'Manager', 60000),
(2, 'Jane Smith', 'Developer', 50000);

Step 3: Add a Data Source

Use a SqlDataSource to connect to the database.

asp
Copy code
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDB %>"
SelectCommand="SELECT * FROM Employees">
</asp:SqlDataSource>

Step 4: Design the FormView Control

The FormView control displays one record at a time and allows inserting, updating, and deleting.

asp
Copy code
<asp:FormView ID="FormView1" runat="server"
DataSourceID="SqlDataSource1"
DefaultMode="ReadOnly">

Step 5: Design the Repeater Control

The Repeater is used to display multiple records in a customizable format.

asp
<h3>Employee List</h3>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<HeaderTemplate>
<table border="1">
<tr>
<th>Name</th>
<th>Position</th>
<th>Salary</th>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr>
<td><%# Eval("Name") %></td>
<td><%# Eval("Position") %></td>
<td>$<%# Eval("Salary") %></td>
</tr>
</ItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

Step 6: Run the Application

1. Press F5 or click Run in Visual Studio.


2. The FormView will show a single employee's details.
3. The Repeater will display a list of all employees.

You might also like