Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

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.

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Data.SqlClient.SqlCommandBuilder

[Visual Basic]
NotInheritable Public Class SqlCommandBuilder
   Inherits Component
[C#]
public sealed class SqlCommandBuilder : Component
[C++]
public __gc __sealed class SqlCommandBuilder : public Component
[JScript]
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.

Remarks

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.

Example

[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)

    myConn.Open()

    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)

    myConn.Close()
End Function 'SelectSqlSrvRows

[C#] 
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);

   myConn.Open();

   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);

   myConn.Close();

   return ds;
}

[C++] 
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);

        myConn->Open();

        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);

        myConn->Close();

        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.

Requirements

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

Show:
© 2015 Microsoft