C#-Unit5
C#-Unit5
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.
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.
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.
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.
Converts Intermediate Language (IL or MSIL) into machine code at runtime, improving performance.
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:
Retrieving data
SqlDataSource:
Supports various SQL database types (e.g., SQL Server, Oracle, OLE DB, ODBC).
</asp:SqlDataSource>
ObjectDataSource:
Connects to and retrieves data from business objects or methods that return data.
Example:
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
TypeName="MyNamespace.EmployeeService"
SelectMethod="GetEmployees">
</asp:ObjectDataSource>
EntityDataSource:
AccessDataSource:
LinqDataSource:
Enables use of LINQ (Language Integrated Query) to retrieve and modify data from a data object.
asp
Copy code
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDB %>"
SelectCommand="SELECT * FROM Employees">
</asp:SqlDataSource>
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">
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>