What is the use of SqlCommandBuilder?

 Posted by Ddd on 1/20/2011 | Category: ADO.NET Interview questions | Views: 25071 | Points: 40
Answer:

It is a class defined by System.Data.SqlClient namespace and is used to generate commands(insert,delete,update) that can be used

to reflect DataSet changes back to a sql server database

Update method of SqlDataAdapter uses the SqlCommandBuilder's commands
to reflect DataSet changes back to a sql server database.


example:

//save a record in a database using the Update method of SqlDataAdapter
using System.Data;
using System.Data.SqlClient;

class dd
{
static void Main()
{
SqlConnection cn=new SqlConnection("server=.;uid=sa;pwd=1234;database=employee");
SqlDataAdapter da=new SqlDataAdapter("select * from emp",cn);
DataTable dt=new DataTable();
da.Fill(dt);
//emp has 2 columns known as eno and ename.

//Initialize the SqlCommandBuilder.
SqlCommandBuilder cd=new SqlComandBuilder(da);

//create a DataRow
DataRow dr=dt.NewRow();
dr["eno"]=100;
dr["ename"]="king";
//the row is temporarily saved
dt.Rows.Add(dr);

//save the Row permanently
da.Update(dt);

}
}


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response