Connection Oriented Architecture In ADO.NET

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:
 
ADO.NET
 
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:
  1. SqlConnection con=new SqlConnection(“Integrated security=true;initial catalog=Student;Data source=.”);   
Or
  1. 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:
  1. 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:
 
DataReader
 
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
  1. <%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="Student.aspx.cs"Inherits="Iare.Student"%>  
  2. <!DOCTYPE html>  
  3. <html  
  4.     xmlns="http://www.w3.org/1999/xhtml">  
  5.     <head runat="server">  
  6.         <title></title>  
  7.         <styletype="text/css">  
  8.           .auto-style1 {  
  9.           width: 100%;  
  10.           }  
  11.   
  12.         </style>  
  13.     </head>  
  14.     <body style="height: 198px; width: 364px">  
  15.         <form id="form1"runat="server">  
  16.             <table class="auto-style1">  
  17.                 <tr>  
  18.                     <tdcolspan="2">  
  19.                         <asp:DropDownListID="DropDownList1"runat="server"DataSourceID="Iare"DataTextField="SId"DataValueField="SId">  
  20.                         </asp:DropDownList>  
  21.                         <asp:SqlDataSourceID="Iare"runat="server"ConnectionString="<%$ ConnectionStrings:IareConnectionString %>"SelectCommand="SELECT [SId] FROM  [Library]">  
  22.                         </asp:SqlDataSource>  
  23.                     </td>  
  24.                 </tr>  
  25.                 <tr>  
  26.                     <td>  
  27.                         <asp:LabelID="Label1"runat="server"Text="Student First Name Is :">  
  28.                         </asp:Label>  
  29.                     </td>  
  30.                     <td>  
  31.                         <asp:TextBoxID="TextFName"runat="server">  
  32.                         </asp:TextBox>  
  33.                     </td>  
  34.                 </tr>  
  35.                 <tr>  
  36.                     <td>  
  37.                         <asp:LabelID="Label2"runat="server"Text="Student Last Name Is :">  
  38.                         </asp:Label>  
  39.                     </td>  
  40.                     <td>  
  41.                         <asp:TextBoxID="TextLName"runat="server">  
  42.                         </asp:TextBox>  
  43.                     </td>  
  44.                 </tr>  
  45.                 <tr>  
  46.                     <td>  
  47.                         <asp:LabelID="Label3"runat="server"Text="Student Is Study :">  
  48.                         </asp:Label>  
  49.                     </td>  
  50.                     <td>  
  51.                         <asp:TextBoxID="TextCourse"runat="server">  
  52.                         </asp:TextBox>  
  53.                     </td>  
  54.                 </tr>  
  55.                 <tr>  
  56.                     <td>  
  57.                         <asp:LabelID="Lbldsply"runat="server">  
  58.                         </asp:Label>  
  59.                     </td>  
  60.                     <td>  
  61.                         <asp:ButtonID="Button1"runat="server"OnClick="Button1_Click"Text="Save"/>  
  62.                     </td>  
  63.                 </tr>  
  64.             </table>  
  65.             <div></div>  
  66.         </form>  
  67.     </body>  
  68. </html>  
Student Application
 
Figure: Student Application
 
After designing the application go to Student.aspx.cs and write the following code.
 
Student.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9. namespace Iare {  
  10.     public partial classStudent: System.Web.UI.Page {  
  11.         SqlConnection con = newSqlConnection("integrated security=true;Initial Catalog=Iare;Data Source=.");  
  12.         SqlCommand cmd;  
  13.         protected void Page_Load(object sender, EventArgs e) {  
  14.   
  15.         }  
  16.   
  17.         protected void Button1_Click(object sender, EventArgs e) {  
  18.             string s = "insert into Student values(@p1,@p2,@p3,@p4)";  
  19.             con.Open();  
  20.             cmd = newSqlCommand(s, con);  
  21.             cmd.CommandType = CommandType.Text;  
  22.             cmd.Parameters.AddWithValue("@p1", TextFName.Text);  
  23.             cmd.Parameters.AddWithValue("@p2", TextLName.Text);  
  24.             cmd.Parameters.AddWithValue("@p3", TextCourse.Text);  
  25.             cmd.Parameters.AddWithValue("@p4", DropDownList1.SelectedItem.Value);  
  26.             cmd.ExecuteNonQuery();  
  27.             con.Close();  
  28.             Lbldsply.Text = "Student Details Has Saved";  
  29.         }  
  30.     }  
Now run the application and check.
 
Read more articles on ADO.NET:


Similar Articles