Thursday 2 January 2014

What is ADO.NET ? (Part - 1)


What is ADO.NET ? 

What is ADO.NET?

ADO.NET is not a different technology. In simple terms, you can think of ADO.NET, as a set of classes (Framework), that can be used to interact with data sources like Databases and XML files. This data can, then be consumed in any .NET application. ADO stands for Microsoft ActiveX Data Objects.

The following are, a few of the different types of .NET applications that use ADO.NET to connect to a database, execute commands, and retrieve data.
ASP.NET Web Applications
Windows Applications
Console Applications

What are .NET Data Providers?
Databases only understand SQL. If a .NET application (Web, Windows, Console etc..) has to retrieve data, then the application needs to
1. Connect to the Database
2. Prepare an SQL Command
3. Execute the Command
4. Retrieve the results and display in the application

Sample ADO.NET code to connect to SQL Server Database and retrieve data.Notice that we are using SQLConnection, SQLCommand and SQLDataReaderclasses . All the objects are prefixed with the word SQL. All these classes are present inSystem.Data.SqlClient namespace. So, we can say that the .NET data provider for SQL Server is System.Data.SqlClient.

------
Step 1.Take an Grid View In Webform

--------------------------------------------------C# Code -------------------------------------------------
Step 2.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace WebApplication2
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("data source=.; database=Sample; integrated security=SSPI");

            // Here "." indicates connect tolocal instance of sql server
            // database = database name in sequal server 
            //SSIP means we are using windows authentication

            SqlCommand cmd = new SqlCommand("Select * from tblProduct", con);
            // tblProduct = table from the sample database and give connection to the database con
            con.Open();
            // opening the connection 
            SqlDataReader rdr = cmd.ExecuteReader();
            // ExecuteReader() means retreving the data from database
            GridView1.DataSource = rdr; // assigning the data to gridview
            GridView1.DataBind();
            con.Close();
        }
    }
}

---------------------------------------------END OF C# CODE-----------------------------------------

OUTPUT 



0 comments:

Post a Comment