.NET Framework Class Library
SqlCommandBuilder Class

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

Namespace:  System.Data.SqlClient
Assembly:  System.Data (in System.Data.dll)
Syntax

Visual Basic (Declaration)
Public NotInheritable Class SqlCommandBuilder _
    Inherits DbCommandBuilder
Visual Basic (Usage)
Dim instance As SqlCommandBuilder
C#
public sealed class SqlCommandBuilder : DbCommandBuilder
Visual C++
public ref class SqlCommandBuilder sealed : public DbCommandBuilder
JScript
public final class SqlCommandBuilder extends DbCommandBuilder
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 been retrieved, such as 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 one or more 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.

Examples

The following example uses the SqlCommand, along SqlDataAdapter and SqlConnection, to select rows from a data source. The example is passed 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
Private Function SelectSqlRows(ByVal connectionString As String, _
    ByVal queryString As String, ByVal tableName As String) As DataSet

    Using connection As New SqlConnection(connectionString)

        Dim adapter As New SqlDataAdapter()
        adapter.SelectCommand = New SqlCommand(queryString, connection)
        Dim builder As SqlCommandBuilder = New SqlCommandBuilder(adapter)

        connection.Open()

        Dim dataSet As 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
    End Using
End Function
C#
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;
    }
}
Inheritance Hierarchy

System..::.Object
  System..::.MarshalByRefObject
    System.ComponentModel..::.Component
      System.Data.Common..::.DbCommandBuilder
        System.Data.SqlClient..::.SqlCommandBuilder
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.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
See Also

Reference

Other Resources

Tags :


Community Content

boban.s
Possible error in code sample

I believe that UpdateCommand should be set for the adapter variable:

builder.GetUpdateCommand();


should be:

adapter.UpdateCommand = builder.GetUpdateCommand();
Tags :

Page view tracker