Data Adapter

Ganeshji
Posted by in ADO.NET category on for Beginner level | Points: 250 | Views : 13839 red flag
Rating: 4.33 out of 5  
 3 vote(s)

Data Adapter is one of the component of Data Provider.

Introduction

 
  Data Provider has 4 components. Among these 4 components, i have covered Data Adapter and two of its methods.

  A Data Adapter is a bridge between the Data Source and the data set.Select,Insert,Update,Delete operations in the Data Source can be performed by data adapter. A Data Adapter contains a connection object and a command object. It manages data in a disconnected mode. And opens and closes connection automatically, when reading or writing to a database.



Fill()

Suppose, i have this table tblStock. Using the Fill() method, one can fetch the records from the Data Source.




From this interface, simply click the Show Button.



Output:




Code behind the Show Button.




Update()


Now suppose, i want to update the number of units in the tblStock table.
Suppose, i have entered 2 for Product Id box and 3 for Units. Now click the Update Button.



Code behind the Update Button.




The following table shows, record has been updated successfully. Previously, it was 20, but now it is only 17 for prod_id=2.







If i just comment the SqlCommandBuilder cmdBuilder=new SqlCommandBuilder(da);
The following error comes. SqlCommandBuilder object is actually used to automatically generate Transact-SQL statements for single-table updates .





Now, i am going to insert a new record into the Data Source using the Update() method.
Enter Product_Id and Number of Units of my own choice and click the Insert button.




Code behind the Insert Button.




A new record is inserted at the end of the table tblStock.



Conclusion

Happy Reading!

Page copy protected against web site content infringement by Copyscape

About the Author

Ganeshji
Full Name: Zinnia Sarkar
Member Level:
Member Status: Member
Member Since: 7/24/2010 12:50:40 PM
Country: India
Regards, Ganeshji


Login to vote for this post.

Comments or Responses

Posted by: Hassan008 on: 1/31/2011 | Points: 25
i assume that by passing dataset to dataadapter you are re-inserting all the values that are in the dataset.
Posted by: Samarmir on: 1/31/2011 | Points: 25
Thanks for the article!
Would it be possible to make the same using LINQ?
Posted by: Anweshabhowmick on: 2/21/2011 | Points: 25
Mam,

This is a very good article and help me too.I have a question for this article.
1.What is the use of CommandBuilder();
2.What is "Products" here.Is this the name of The Table.If so then What is the tblStock.
3.What is the meaning of code da.Update.Why use use this?

and one more query that is the same as "How to add dataTable with DataSet?"

Thank you
Posted by: Ganeshji on: 2/25/2011 | Points: 25
Hallo Anwesha! I m sry to give u late reply for our broadband disorder. Data Adapter has two methods 1. Fill() and 2. Update(). Using Update() method, we can update it in the datasource. Here "Products" is a Data Table within my DataSet. And tblStock is an existing table in SQL SERVER from which i m fetching data and into which i m updating data.
Posted by: Akiii on: 8/16/2011 | Points: 25
Hi,
DataSet ds = new DataSet("Products");


Is it necessary to give table-name while you create dataset object ? Because you are already specifying the table-name in the da.fill() method ?

Regards
Akiii

Login to post response

Comment using Facebook(Author doesn't get notification)