What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 22284 |  Welcome, Guest!   Register  Login
 Home > Interview Questions > ADO.NET Interview Questions > What is the use of the CommandBuilder cl ...

What is the use of the CommandBuilder class ?

Interview question and answer by: Bharathi Cherukuri | Posted on: 5/21/2012 | Category: ADO.NET Interview questions | Views: 1152 | | Points: 40


Answer:

The CommandBuilder class is used to automatically update a database according to the changes made in a DataSet.

This class automatically registers itself as an event listener to the RowUpdating event. Whenever data inside a row changes, the object of the CommandBuilder class automatically generates a SQL statement and uses the SelectCommand property to commit the changes made in DataSet.

For Example, OLEDB provider in .NET Framework has the OleDbCommandBuiider class; whereas, the SQL provider has the SqlCommandBuilder class.

Asked In: Many Interviews | Alert Moderator 
Found interesting? Add this to:


 Responses

Posted by: Akiii | Posted on: 23 May 2012 11:12:01 PM | Points: 10 | Alert Moderator 

Good explanation. Can you provide a sample code ?


Thanks and Regards
Akiii

Posted by: Bharathi Cherukuri | Posted on: 24 May 2012 01:27:55 AM | Points: 10 | Alert Moderator 

Hi Akiii,

Here is the sample code for CommandBuilder class.

Example:

public static DataSet SelectSqlRows(string connectionString,

string queryString, string tableName)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(queryString, connection);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);

connection.Open();

DataSet dataSet = new DataSet();
adapter.Fill(dataSet, tableName);

//code to modify data in DataSet here

builder.GetUpdateCommand();

//Without the SqlCommandBuilder this line would fail
adapter.Update(dataSet, tableName);

return dataSet;
}
}


Regards,
Bharathi

>> Write Response - Respond to this post and get points

Even more ... | Submit Interview Questions and win prizes!

More Interview Questions from Bharathi Cherukuri

Even more ... | Submit Interview Questions and win prizes!


About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/22/2013 3:26:28 AM