Sunday, 29 December 2013

ActiveX Data Object.NET (ADO.NET)

ADO.NET  is a data access component introduced in the Microsoft.Net framework. ADO.NET  includes .NET Framework data providers for connecting to a database, executing commands and retrieving results. ADO.NET uses Extensible Markup Language (XML) for transmitting data between different application layers.

ADO.NET Architecture
  • Presentation Tier works with the interface part of the application that is presented to the client.

  • Business Tier works with the business logic part of the application where all the calculation and logic resides.

  • Data Tier works with the database part of the application.

The Object Model of ADO.NET
  • DataSets

  • .NET Data Providers
DataSet:

DataSet is an object that store retrieved data from the database. The data in the DataSet is stored in the form of a DataTable object. The DataSet can contain one or more tables. The DataTable object in the DataSet  represents a table retrievd from the database. The data in the DataTable is represented by the DataRow object. The DataSet can hold data from a database as well as non database data source. The DataSet does not interact with any database, it is just a container for the data.

Syntax to add table to the DataSet:- 

<DataSet name>.Tables.Add(<DataTable Object name>);

Syntax to add Columns to the DataTable:- 

<DataTable Object name>.Columns.Add(<Column name>,typeof(DataType));

Syntax to add Rows to the DataTable:- 

<DataTable Object name>.Rows.Add();

.Net Data Providers:

The .NET Data Provider contains objects that provide access to data from various data sources. The data can be manipulated through these data providers.

The .NET Data Providers is a collection of components such as:
  • Connection
  • Command
  • DataReader
  • DataAdapter 
There are four types of .NET Data Providers available-
  •  .Net Data Provider for SQL Server
  •  .Net Data Provider for OLEDB
  •  .Net Data Provider for ODBC
  •  .Net Data Provider for Oracle
Connection Object: 

The Connection Object is used to establish a connection between the application and the database.

Connection Object for SQL.NET Data Provider:

Namespaces needed:
  • <%@ Import Namespace="System.Data" %>
  • <%@ Import Namespace="System.Data.SqlClient" %>
Connection String:

SqlConnection connection=new SqlConnection("server=myDB; uid=sa; pwd=password; database=master");

where,
connection is the name of the connection object.
myDB is the name of the SQL Server that stores the database 'master'.
uid is the identity that is used to access the database.
pwd is the password that is required to access the database.

Connection Object for OLEDB.NET Data Provider:

Namespaces needed:
  • <%@ Import Namespace="System.Data" %>
  • <%@ Import Namespace="System.Data.OleDb" %>
Connection String:

OleDbConnection oleconnection=new OleDbConnection("provider=SQLDB; Data Source=myDB; User Id=sa; pwd=password; Initial Catalog=master");

where,
oleconnection is the name of the connection object.
SQLDB is the OLE DB Provider for SQL Server.
myDB is the name of the SQL Server.
Initial Catalog is used to specify the database. 
User Id is the identity that is used to access the database.
pwd is the password that is required to access the database.

Command Object:

The Command Object is used to retrieve, update, insert and delete data in a database.

Retrieving Data:
  • SQL.NET Data Provider
SqlDataAdapter dataadapter=newSqlDataAdapter("select * from student",connection);
        
SqlDataAdapter is the Command Object, dataadapter is the name of the command object and connection is the connection object.
  • OLEDB.NET Data Provider
OleDbDataAdapter dataadapter=new OleDbDataAdapter("select * from student",oleconnection);

OleDbDataAdapter is the Command Object, dataadapter is the name of the command object and oleconnection is the connection object.

Example of DataSet using DataAdapter:

using System.Data;
using System.Data.SqlClient;


namespace Student
{
  public partial class Profile : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      SqlConnection connection = new SqlConnection("server=myDB; uid=sa; pwd=password;
      database=master");
      connection.Open();
       SqlCommand command = new SqlCommand("select * from student", connection);
       SqlDataAdapter da = new SqlDataAdapter();
       da.SelectCommand = command;
       DataSet ds = new DataSet();
       da.Fill(ds, "student");
       studentGrid.DataSource = ds.Tables["student"].DefaultView;
       studentGrid.DataBind();
       connection.Close();

Inserting Data:
  • SQL.NET Data Provider
SqlCommand command=new SqlCommand("insert into student values(1,'Mike','30-12-1990')",connection);
  • OLEDB.NET Data Provider
OleDbCommand olecommand=new OleDbCommand("insert into student values(1,'Mike','30-12-1990')",oleconnection);

Updating Data:
  • SQL.NET Data Provider
SqlCommand command=new SqlCommand("update student set Roll_No=10 where Student_Name='Mike'",connection);
  • OLEDB.NET Data Provider
OleDbCommand olecommand=new OleDbCommand("update student set Roll_No=10 where Student_Name='Mike'",oleconnection);

Deleting Data:
  • SQL.NET Data Provider
SqlCommand command=new SqlCommand("delete from student where Roll_No=10",connection);
  • OLEDB.NET Data Provider
OleDbCommand olecommand=new OleDbCommand("delete from student where Roll_No=10",oleconnection);

DataReader

DataReader is used to view the records one after the other. DataReader is the read-only view of the query result.

DataView

DataView is used to filter the records. It presents customized view of data from a DataTable.

Example:


using System.Data;
using System.Data.SqlClient;


namespace Student
{
   public partial class Profile : System.Web.UI.Page
   {
     protected void Page_Load(object sender, EventArgs e)
     {
       SqlConnection connection = new SqlConnection("server=myDB; uid=sa; pwd=password;
       database=master");
       connection.Open();
       SqlCommand command = new SqlCommand("select Roll_No,Student_Name,DOB from 
       Student", connection);
       DataTable dt = new DataTable();
          
       dt.Columns.Add("Roll No");
       dt.Columns.Add("Student Name");
       dt.Columns.Add("Date of Birth");
       DataRow dr = dt.NewRow();

       SqlDataReader dataReader = command.ExecuteReader();

          while (dataReader.Read())
            {
                dr["Roll No"] = dataReader.GetValue(0).ToString();
                dr["Student Name"]=dataReader.GetValue(1).ToString();
                dr["Date of Birth"] = dataReader.GetValue(2).ToString();
                dt.Rows.Add(dr);
                dr = dt.NewRow();
                studentGrid.DataSource = dataReader;
                studentGrid.DataBind();
            }
               dataReader.Close();
               connection.Close();
        }
    }
}

No comments:

Post a Comment