Introduction
ADO stands for active data object, which behaves as a mediator between the client side and server side. Client side can’t directly interact withthe server side, so there is
ADO.NET which behaves as a mediator between front end and back end as in the following figure:
Figure: ADO.NET
The preceding image shows the client-side application interacts with the server-side application through ADO.NET which is behaving as a mediator between the front end and back end.
Note: A programming language does not understand syntax and structure of other programming language but if it is required to interact from one to another programming language, there will be one mediator which will help them to interact without any problem.
Connection-Oriented in ADO.NET
Before discussing ADO.NET connection orientation we will discuss what ADO.NET contains.
ADO.NET is a group of class which contains.
- Connection
- Command
- Datareader
- DataAdapter
- Dataset
Connection
Connection class used to establish the connection between the front end and back end:
- SqlConnection con=new SqlConnection(“Integrated security=true;initial catalog=Student;Data source=.”);
Or
- SqlConnection Con=new SqlConnection(“User id=sa;Password=sa123;Database=Student;Server=.”);
Command
Command class behaves as a bridge between the front end and back end. It contains the query which has to perform from the front end and back end and also it contains an object of Connection Class:
- SqlCommand cmd=new SqlCommand(“Query which has to perform”,Connection Object);
- DataReader: DataReader used to read the data from the source.
- Dataset: DataSet contains the table and relation.
- DataAdapter: DataAdapter behaves as a mediator between the back end and front end. But it does not have features to contains the data. So there is a dataset that contains the data of the result set.
In connection-oriented architecture the Database gets connected to the front end then the command passes the query to the server from the back end and on the server the result which has been generated. The result which has been generated will be read by DataReader. Here's the screenshot:
The above figure has shown that the connection-oriented architecture of ADO.NET has Connection, Command, and DataReader used to establish the connection between the front end and back end and passing the query from the front end to the back end.
Sample code
Write the code for design or use drag down to design the application.
Student.aspx
- <%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="Student.aspx.cs"Inherits="Iare.Student"%>
- <!DOCTYPE html>
- <html
- xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <styletype="text/css">
- .auto-style1 {
- width: 100%;
- }
-
- </style>
- </head>
- <body style="height: 198px; width: 364px">
- <form id="form1"runat="server">
- <table class="auto-style1">
- <tr>
- <tdcolspan="2">
- <asp:DropDownListID="DropDownList1"runat="server"DataSourceID="Iare"DataTextField="SId"DataValueField="SId">
- </asp:DropDownList>
- <asp:SqlDataSourceID="Iare"runat="server"ConnectionString="<%$ ConnectionStrings:IareConnectionString %>"SelectCommand="SELECT [SId] FROM [Library]">
- </asp:SqlDataSource>
- </td>
- </tr>
- <tr>
- <td>
- <asp:LabelID="Label1"runat="server"Text="Student First Name Is :">
- </asp:Label>
- </td>
- <td>
- <asp:TextBoxID="TextFName"runat="server">
- </asp:TextBox>
- </td>
- </tr>
- <tr>
- <td>
- <asp:LabelID="Label2"runat="server"Text="Student Last Name Is :">
- </asp:Label>
- </td>
- <td>
- <asp:TextBoxID="TextLName"runat="server">
- </asp:TextBox>
- </td>
- </tr>
- <tr>
- <td>
- <asp:LabelID="Label3"runat="server"Text="Student Is Study :">
- </asp:Label>
- </td>
- <td>
- <asp:TextBoxID="TextCourse"runat="server">
- </asp:TextBox>
- </td>
- </tr>
- <tr>
- <td>
- <asp:LabelID="Lbldsply"runat="server">
- </asp:Label>
- </td>
- <td>
- <asp:ButtonID="Button1"runat="server"OnClick="Button1_Click"Text="Save"/>
- </td>
- </tr>
- </table>
- <div></div>
- </form>
- </body>
- </html>
Figure: Student Application
After designing the application go to
Student.aspx.cs and write the following code.
Student.aspx.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Data.SqlClient;
- namespace Iare {
- public partial classStudent: System.Web.UI.Page {
- SqlConnection con = newSqlConnection("integrated security=true;Initial Catalog=Iare;Data Source=.");
- SqlCommand cmd;
- protected void Page_Load(object sender, EventArgs e) {
-
- }
-
- protected void Button1_Click(object sender, EventArgs e) {
- string s = "insert into Student values(@p1,@p2,@p3,@p4)";
- con.Open();
- cmd = newSqlCommand(s, con);
- cmd.CommandType = CommandType.Text;
- cmd.Parameters.AddWithValue("@p1", TextFName.Text);
- cmd.Parameters.AddWithValue("@p2", TextLName.Text);
- cmd.Parameters.AddWithValue("@p3", TextCourse.Text);
- cmd.Parameters.AddWithValue("@p4", DropDownList1.SelectedItem.Value);
- cmd.ExecuteNonQuery();
- con.Close();
- Lbldsply.Text = "Student Details Has Saved";
- }
- }
- }
Now run the application and check.
Read more articles on ADO.NET: