This documentation is archived and is not being maintained.

SqlCommandBuilder Class

Automatically generates single-table commands used to reconcile changes made to a DataSet with the associated SQL Server database. This class cannot be inherited.

For a list of all members of this type, see SqlCommandBuilder Members.


[Visual Basic]
NotInheritable Public Class SqlCommandBuilder
   Inherits Component
public sealed class SqlCommandBuilder : Component
public __gc __sealed class SqlCommandBuilder : public Component
public class SqlCommandBuilder extends Component

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


The SqlDataAdapter does not automatically generate the Transact-SQL statements required to reconcile changes made to a DataSet with the associated instance of SQL Server. However, you can create a SqlCommandBuilder object to automatically generate Transact-SQL statements for single-table updates if you set the SelectCommand property of the SqlDataAdapter. Then, any additional Transact-SQL statements that you do not set are generated by the SqlCommandBuilder.

The SqlCommandBuilder registers itself as a listener for RowUpdating events whenever you set the DataAdapter property. You can only associate one SqlDataAdapter or SqlCommandBuilder object with each other at one time.

To generate INSERT, UPDATE, or DELETE statements, the SqlCommandBuilder uses the SelectCommand property to retrieve a required set of metadata automatically. If you change the SelectCommand after the metadata has is retrieved (for example, after the first update), you should call the RefreshSchema method to update the metadata.

The SelectCommand must also return at least one primary key or unique column. If none are present, an InvalidOperation exception is generated, and the commands are not generated.

The SqlCommandBuilder also uses the Connection, CommandTimeout, and Transaction properties referenced by the SelectCommand. The user should call RefreshSchema if any of these properties are modified, or if the SelectCommand itself is replaced. Otherwise the InsertCommand, UpdateCommand, and DeleteCommand properties retain their previous values.

If you call Dispose, the SqlCommandBuilder is disassociated from the SqlDataAdapter, and the generated commands are no longer used.


[Visual Basic, C#, C++] The following example uses the SqlCommand, along SqlDataAdapter and SqlConnection, to select rows from a data source. The example is passed an initialized DataSet, a connection string, a query string that is a Transact-SQL SELECT statement, and a string that is the name of the database table. The example then creates a SqlCommandBuilder.

[Visual Basic] 
Public Shared Function SelectSqlSrvRows(myConnection As String, mySelectQuery As String, myTableName As String) As DataSet
    Dim myConn As New SqlConnection(myConnection)
    Dim myDataAdapter As New SqlDataAdapter()
    myDataAdapter.SelectCommand = New SqlCommand(mySelectQuery, myConn)
    Dim cb As SqlCommandBuilder = New SqlCommandBuilder(myDataAdapter)


    Dim ds As DataSet = New DataSet
    myDataAdapter.Fill(ds, myTableName)

    ' Code to modify data in DataSet here 

    ' Without the SqlCommandBuilder this line would fail.
    myDataAdapter.Update(ds, myTableName)

End Function 'SelectSqlSrvRows

public static DataSet SelectSqlSrvRows(string myConnection, string mySelectQuery, string myTableName)
   SqlConnection myConn = new SqlConnection(myConnection);
   SqlDataAdapter myDataAdapter = new SqlDataAdapter();
   myDataAdapter.SelectCommand = new SqlCommand(mySelectQuery, myConn);
   SqlCommandBuilder cb = new SqlCommandBuilder(myDataAdapter);


   DataSet ds = new DataSet();
   myDataAdapter.Fill(ds, myTableName);

   //code to modify data in DataSet here

   //Without the SqlCommandBuilder this line would fail
   myDataAdapter.Update(ds, myTableName);


   return ds;

    static DataSet* SelectSqlSrvRows(String* myConnection, String* mySelectQuery, String* myTableName)
        SqlConnection* myConn = new SqlConnection(myConnection);
        SqlDataAdapter* myDataAdapter = new SqlDataAdapter();
        myDataAdapter->SelectCommand = new SqlCommand(mySelectQuery, myConn);
        SqlCommandBuilder* cb = new SqlCommandBuilder(myDataAdapter);


        DataSet* ds = new DataSet();
        myDataAdapter->Fill(ds, myTableName);

        //code to modify data in DataSet here

        //With->Item[Out] the* SqlCommandBuilder this line would fail
        myDataAdapter->Update(ds, myTableName);


        return ds;

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Namespace: System.Data.SqlClient

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Data (in System.Data.dll)

See Also

SqlCommandBuilder Members | System.Data.SqlClient Namespace