Use of sqlcommandBuilder and sqlcommand
my question is that when we will use sqlcommandBuilder and when we will use sqlcommand ...please me
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Veena SardaRank #79 · 24.5K reputationPosted Oct 7, 2013, 8:29 PM EDT
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Oct 7, 2013, 8:30 PM EDT
a) SQLCommand is used to execute all kind of SQL queries like DML(Insert update Delete) & DDL like(Create table drop table etc)
b)SQLCommandBuilder object is used to build & execute SQL (DML) queries like select insert update & delete.
The SqlCommand encapsulates all the required informations to execute the command which includes:
- Database connection. (see SqlCommand.Connection property)
- the command string like "select * from .... " , see SqlCommand.CommandText property)
- The command type (stored procedures,sql query .... ), see SqlCommand.CommandType property.
- The Command parameters , see SqlCommand.Parameters property.
The SqlCommandBuilder can be used to generate update,insert and delete commands based on the select command.so basically the class will have an internal logic to generate those type of commands for you.Santosh KumarRank #2,303 · 613 reputationPosted Oct 9, 2013, 2:22 AM EDT
SQLCommand
The SQLCommand Object is used to executes SQL statements and Stored Procedures against the data source. It execute all kind of SQL queries like Insert, update etc.
e.g.
SqlCommand cmd = new SqlCommand("your sql statements", Connection); cmd.ExecuteNonQuery();
SqlCommandBuilder
The SqlCommandBuilder can be used to build and execute SQL queries based on the select command that you will supply. It provides the feature of reflecting the changes made to a DataSet or an instance of the SQL server data. The CommandBuilder opens the Connection associated with the DataAdapter Object and makes a round trip to the server each and every time and it's asked to construct the action queries.
The SqlCommandBuilder object acts as a listener for RowUpdating events, whenever the DataAdapter property is set. You can create a SqlCommandBuilder object to automatically generate SQL statements for single table updates if you set the SelectCommand property of the SqlDataAdapter.
e.g.
SqlDataAdapter adapter = new SqlDataAdapter("your sql statement", connection); SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(adapter); adapter.Fill(ds);
//Here you can do any modification to the selected data
ds.Tables[0].Rows[i].ItemArray[2] = Convert.ToInt32 (ds.Tables[0].Rows[i].ItemArray[2]) + 100;
//Here save the data to the datasource
adapter.Update(ds.Tables[0]);