Overview Of ADO.NET

ADO.NET is a bundle of classes which stands for ActiveX data object. It is used to get the data from Back end to Front end.

Step to work with Ado .NET

  1. Establish the Connection
  2. Pass statement from front end to back end
  3. Close the Connection

Classes which work with ADO.NET

  • SqlConnection
  • SqlCommand
  • SqlDataAdapter
  • Dataset

SqlConnection

SqlConnection class used to established the connection between front end and back end.

Syntax:

    SqlConnection obj=new SqlConnection(“Integrated Security=true;Initial Catalog=Table_Name;Data Source=.”);-- for Windows authentication

    SqlConnection obj=new SqlConnection(“user id= sa ; Password=sa123;server=.;database=name”); --Sql server Authentication

SqlCommand

SqlCommand is used to pass the statement from front end to back end by using the following syntax.

Syntax

    SqlCommand objcmd=new SqlCommand(“Query Statement”,Connection object);
SqlDataAdapter

SqlDataAdapter is a mediator between database and dataset. SqlDataAdapter do not have a feature of getting the data directly from the database. Syntax for working with SqlDataAdapter is the following:

Syntax

    SqlDataAdapter objda=new SqlDataAdapter(“query statement”,Connection object);

Dataset

Dataset contains Table and relation as:

Dataset
Figure: Dataset

Data bound Control

Data bound control is used for displaying more than one record at a time. Here are the types of Data bound Control:

  1. Repeater
  2. Datalist
  3. Grid view
  4. Form View
  5. Details View
  6. List View

Data bound control fetch the data record by record and display the data field by field.

Sample Insertion Example for working with ADO.NET

  1. SqlConnection con = new SqlConnection(“Integrate Security = true; Initial Catalog = Employee; data source = .”);  
  2. SqlCommand cmd();  
  3. Private void Page_Load() {}  
  4. Private void btnInsert_Click()   
  5. {  
  6.     Con.open();  
  7.     String s = “Insert into Emp values(@p1, @p2, @p3)”;  
  8.     Cmd = new SqlCommand(s, con);  
  9.     Cmd.CommandType = CommandType.Text;  
  10.     Cmd.Parameters.AddWithValue(“@p1”, txtEid.Text);  
  11.     Cmd.Parameters.AddWithValue(“@p2”, txtEname.Text);  
  12.   
  13.     Cmd.Parameters.AddWithValue(“@p1”, txtsalary.Text);  
  14.     Int i = cmd.ExecuteNon Query();  
  15.     Con.Close();  
  16. }  
  17.    Messagebox.Show(i + “Rows are Inserted”);  
  18. }  
Sample Example code for displaying the data in GridView:
  1. Using System.Data;  
  2. Using System.Data.SqlClient;  
  3. SqlConnection con = new Sqlconnection(“Integrate Security = true; Initial Catalog = Employee; data source = .”);  
  4. SqlDataAdapter Da();  
  5. Private void Page_Load()   
  6. {  
  7.     String s = ”select * from Emp”;  
  8.     Da = new SqlDataAdapter(s, con);  
  9.     Dataset ds = new Dataset();  
  10.     Da.fill(ds, ”Empobj”);  
  11.     Gridview = ds.tables[0];  
  12.     GridView.DataBound();  
  13. }  

Up Next
    Ebook Download
    View all
    Learn
    View all