Sunday, November 24, 2013

OBJECTS OF ADO.NET

OBJECTS OF ADO.NET

1. sqlconnection
2.  sqlcommand
3.  sqldatareader
4.  sqldataadapter
5.  dataset 
6.  datatable

as we see the sqlconnection object in the ado.net section here, http://aspfunlearning.blogspot.in/2013/11/adonet-and-its-objects.html

here we see the other objects and its descriptions.

SQLCOMMAND

     The sqlcommand is used to communicate the with the database. we know that sql query is used with database hence that query we use as a parameter along with the connection string.

ex: 
         sqlcommand sqlCom = new sqlcommand(sql query,connectionstring);

here we create an instance of a sqlcommand object with name sqlCom and it will take two parameters sqlquery and connection string.

SQLDATAREADER

               The sqldatareader is used to read the value row by row from the datatable and when we use this we need to specify the con.open() and con.close() after the function.

ex:

               SqlDataReader dr = cmd.ExecuteReader();

SQLDATAADAPTER 

                  The sqldataadapter is act as a bridge between dataset and database. It gets the whole value of a database and stores in a dataset for our operational use, the dataset object we see in next paragraph.

ex:

              sqldataadapter sqlDa = new dataadapter (sqlquery,connection string);

DATASET

               The dataset is the object which used to store the database value to a n no. of data table , the dataset is a collection of data table.

ex: 

          dataset ds = new dataset();   // declaring dataset

          sqlDa.Fill(ds);    // here we fill the dataset with the database value

DATATABLE

                The datatable is a split content or subset of dataset which means collection of data table we use to store it into datatable.

ex:
           datatable dt = new datatable();     //declaring datatable   for single table
           sqlDa.Fill(dt);     // here we fill the datatable with the database  value 

example with all above said ado.net objects, for retrieving the value from a table,

      sqlconnection sqlCon = new sqlCon("Data Source=G-V-R\\SQLEXPRESS;Initial Catalog = RamAnand;Integrated Security=True");

     sqlcommand sqlCom = new sqlcommand("select * from tablename",sqlCon);

    sqldataadapter sqlDa = new dataadapter (sqCom);

    dataset ds = new dataset();

    sqlDa.Fill(ds);

after this step, we have to set or assign this ds value to the gridview we created in the asp.net control like,

gridview.datasource = ds;

gridview1.databind(); 

for sqldatareader see my post http://aspfunlearning.blogspot.in/2013/11/sql-data-reader.html

Thank you for use my post and if you learned something then like my post!

No comments:

Post a Comment