Command Builder in ADO.NET

Command Builder:

A command builder is an ADO.NET object that lets you create command objects for a data adapter's insert, update, delete statement is based on the adapter's select statement. that way, you don't have to worry about coding these statements and creating these command yourself.

create a SqlCommandBuilder:  There are two way to create SqlCommandBuilder in ADO.NET. That are:

  • CommandBuilder = new SqlCommandBuilder().

  • CommandBuilder = new SqlCommandBuilder(Data Adapter).

Properties of command builder:

  • DataAdapter: The Data Adapter that contain the select statement that will be used to generate insert, update, delete statements.

  • Refresh Schema: Refresh the schema information that's used to generate the insert, update, delete statements.

Code that creates a SqlCommandBuilder Object:

Imports System.Data.SqlClient 
Module
Module1 
    Sub Main()
        Dim da As New SqlDataAdapter 
        Dim cmdb As New SqlCommandBuilder()
        cmdb.da = "Table_name"
    End Sub
 
End
Module

Description:

  • You can use a command builder to generate Insert, update and delete command for a data adapter from the select command for the data Adapter. for this work, the select command must contain a select statement.

  • The data Adapter value is set to the value that you pass to the constructor for the Command Builder. if you don't pass values, you must set the properties after you create a command builder.

Note: 

  • A command Builder can't be used with a select command that contain the name of a store procedure or a select statement that retrieve data from more than one table.

  • The select statement associate with data adapter must include a primary key or a unique column  from a table.