ADO .Net

ADO  .Net
Written by Sagar RabidasNovember 25, 2021
13 min read
ADO .Net
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Sagar Rabidas

Software Developer

Today we will discuss the above ADO .Net.

ADO .Net

ADO .Net provides a bridge between the front cease controls and the returned give-up database. The ado.Net gadgets encapsulate all of the records get admission to operations and the controls interact with those objects to display facts, as a result hiding the details of the motion of data.

The following figure shows the ADO .NET objects at a glance:

blogpost

The DataSet Class

The dataset represents a subset of the database. It does not have a non-stop connection to the database. To replace the database a reconnection is required. The dataset carries datatable objects and data relation items. The data relation objects represent the relationship between two tables.

The following table shows some important properties of the DataSet class:

blogpost

The following table shows some important methods of the DataSet class:

blogpost

blogpost

The DataTable Class

The DataTable class represents the tables in the database. It has the following important properties; most of these properties are read-only properties except the PrimaryKey property:

  • ChildRelations:-

Returns the collection of child relationships.

  • Columns:-

Returns the Columns collection.

  • Constraints:-

Returns the Constraints collection.

  • DataSet:-

Returns the parent DataSet.

  • DefaultView:-

Returns a view of the table.

  • ParentRelations:-

Returns the ParentRelations collection.

  • PrimaryKey:-

Gets or sets an array of columns as the primary key for the table.

  • Rows:-

Returns the Rows collection.

The following table shows some important methods of the DataTable class:

  • AcceptChanges:-

Commits all changes since the last AcceptChanges.

  • Clear:-

Clears all data from the table.

  • GetChanges:-

Returns a copy of the DataTable with all changes made since the AcceptChanges method was called.

  • GetErrors:-

Returns an array of rows with errors.

  • ImportRows:-

Copies a new row into the table.

  • LoadDataRow:-

Finds and updates a specific row, or creates a new one, if not found any.

  • Merge:-

Merges the table with another DataTable.

  • NewRow:-

Creates a new DataRow.

  • RejectChanges:-

Rolls back all changes made since the last call to AcceptChanges.

  • Reset:-

Resets the table to its original state.

  • Select:-

Returns an array of DataRow objects.

The DataRow Class

The DataRow object represents a row in a table. It has the following important properties:

  • errors:-

Indicates if there are any errors.

  • Items:-

Gets or sets the data stored in a specific column.

  • ItemArrays:-

Gets or sets all the values for the row.

  • Table:-

Returns the parent table.

The following table shows some important methods of the DataRow class:

  • AcceptChanges:-

Accepts all changes made since this method was called.

  • BeginEdit:-

Begins edit operation.

  • canceled:-

Cancels edit operation.

  • Delete:-

Deletes the DataRow.

  • EndEdit:-

Ends the edit operation.

  • GetChildRows:-

Gets the child rows of this row.

  • GetParentRow:-

Gets the parent row.

  • GetParentRows:-

Gets parent rows of DataRow object.

  • RejectChanges:-

Rolls back all changes made since the last call to AcceptChanges.

The DataAdapter Object

The DataAdapter object acts as a mediator between the dataset object and the database. This facilitates the dataset to include data from a couple of databases or other data supplies.

The DataReader Object

The DataReader object is an alternative to the dataset and DataAdapter combination. This object gives a connection-oriented get right of entry to the statistics in the database. These gadgets are appropriate for read-handiest access, such as populating a list and then breaking the relationship.

DbCommand and DbConnection Objects

The DB connection object represents a connection to the statistics supply. The connection could be shared amongst exceptional command gadgets.

The DB command object represents the command or a saved process sent to the database from retrieving or manipulating records.

Example

So far, we have used tables and databases already existing on our computer. In this example, we will create a table, add columns, rows and data into it and display the table using a GridView object.

The source file code is as given:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="createdatabase._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
   <head runat="server">
      <title>
         Untitled Page
      </title>
   </head>
   
   <body>
      <form id="form1" runat="server">
      
         <div>
            <asp:GridView ID="GridView1" runat="server">
            </asp:GridView>
         </div>
         
      </form>
   </body>
   
</html>

The code-behind file is as given:

namespace create database
{
   public partial class _Default : System.Web.UI.Page
   {
      protected void Page_Load(object sender, EventArgs e)
      {
         if (!IsPostBack)
         {
            DataSet ds = CreateDataSet();
            GridView1.DataSource = ds.Tables["Student"];
            GridView1.DataBind();
         }
      }
      
      private DataSet CreateDataSet()
      {
         //creating a DataSet object for tables
         DataSet dataset = new DataSet();
         // creating the student table
         DataTable Students = CreateStudentTable();
         dataset.Tables.Add(Students);
         return dataset;
      }
      
      private DataTable CreateStudentTable()
      {
         DataTable Students = new DataTable("Student");
         // adding columns
         AddNewColumn(Students, "System.Int32", "StudentID");
         AddNewColumn(Students, "System.String", "StudentName");
         AddNewColumn(Students, "System.String", "StudentCity");
         // adding rows
         AddNewRow(Students, 1, "M H Kabir", "Kolkata");
         AddNewRow(Students, 1, "Shreya Sharma", "Delhi");
         AddNewRow(Students, 1, "Rini Mukherjee", "Hyderabad");
         AddNewRow(Students, 1, "Sunil Dubey", "Bikaner");
         AddNewRow(Students, 1, "Rajat Mishra", "Patna");
         return Students;
      }
      private void AddNewColumn(DataTable table, string columnType, string  columnName)
      {
         DataColumn column = table.Columns.Add(columnName,  Type.GetType(columnType));
      }
      //adding data into the table
      private void AddNewRow(DataTable table, int id, string name, string city)
      {
         DataRow newrow = table.NewRow();
         newrow["StudentID"] = id;
         newrow["StudentName"] = name;
         newrow["StudentCity"] = city;
         table.Rows.Add(newrow);
      }
   }
}

ado net
html
html5
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
You must be Logged in to comment
Code Block
Sagar Rabidas
Software Developer
162 Blog Posts
15 Discussion Threads
Trending Categories
15
Software39
DevOps45
Frontend Development24
Backend Development20
Server Administration17
Linux Administration24
Data Center24
Sentry24
Terraform21
Ansible29
Docker28
Penetration Testing14
Kubernetes16
NGINX19
JenkinsX17
Recommended Blogs
1
Techiio

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

facebooklinkdeintwitter

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Copyright techiio.com @2020 Kolkata, India
made with by Abhishek & Priyanka Jalan
Copyright techiio.com @2020
made with by Abhishek & Priyanka Jalan