This documentation is archived and is not being maintained.

SqlDataAdapter.InsertCommand Property

Gets or sets a Transact-SQL statement or stored procedure to insert new records into the data source.

[Visual Basic]
Public Shadows Property InsertCommand As SqlCommand
public new SqlCommand InsertCommand {get; set;}
public: __property SqlCommand* get_InsertCommand();
public: __property void set_InsertCommand(SqlCommand*);
public hide function get InsertCommand() : SqlCommand;
public function set InsertCommand(SqlCommand);

Property Value

A SqlCommand used during Update to insert records into the database that correspond to new rows in the DataSet.


During Update, if this property is not set and primary key information is present in the DataSet, the InsertCommand can be generated automatically if you set the SelectCommand property and use the SqlCommandBuilder. Then, any additional commands that you do not set are generated by the SqlCommandBuilder. This generation logic requires key column information to be present in the DataSet. For more information see Automatically Generated Commands.

When InsertCommand is assigned to a previously created SqlCommand, the SqlCommand is not cloned. The InsertCommand maintains a reference to the previously created SqlCommand object.

Note   If execution of this command returns rows, these rows may be added to the DataSet depending on how you set the UpdatedRowSource property of the SqlCommand object.


[Visual Basic, C#, C++] The following example creates a SqlDataAdapter and sets the SelectCommand and InsertCommand properties. It assumes you have already created a SqlConnection object.

[Visual Basic] 
Public Shared Function CreateCustomerAdapter(conn As SqlConnection) As SqlDataAdapter 
  Dim da As SqlDataAdapter = New SqlDataAdapter()
  Dim cmd As SqlCommand

  ' Create the SelectCommand.

  cmd = New SqlCommand("SELECT * FROM Customers " & _
                       "WHERE Country = @Country AND City = @City", conn)

  cmd.Parameters.Add("@Country", SqlDbType.NVarChar, 15)
  cmd.Parameters.Add("@City", SqlDbType.NVarChar, 15)

  da.SelectCommand = cmd

  ' Create the InsertCommand.

  cmd = New SqlCommand("INSERT INTO Customers (CustomerID, CompanyName) " & _
                       "VALUES (@CustomerID, @CompanyName)", conn)

  cmd.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID")
  cmd.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName")

  da.InsertCommand = cmd

  Return da
End Function

public static SqlDataAdapter CreateCustomerAdapter(SqlConnection conn)
  SqlDataAdapter da = new SqlDataAdapter();
  SqlCommand cmd;

  // Create the SelectCommand.

  cmd = new SqlCommand("SELECT * FROM Customers " +
                       "WHERE Country = @Country AND City = @City", conn);

  cmd.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
  cmd.Parameters.Add("@City", SqlDbType.NVarChar, 15);

  da.SelectCommand = cmd;

  // Create the InsertCommand.

  cmd = new SqlCommand("INSERT INTO Customers (CustomerID, CompanyName) " +
                       "VALUES (@CustomerID, @CompanyName)", conn);

  cmd.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
  cmd.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");

  da.InsertCommand = cmd;

  return da;

static SqlDataAdapter* CreateCustomerAdapter(SqlConnection* conn)
  SqlDataAdapter* da = new SqlDataAdapter();
  SqlCommand* cmd;

  // Create the SelectCommand.

  cmd = new SqlCommand(S"SELECT * FROM Customers " 
                       S"WHERE Country = @Country AND City = @City", conn);

  cmd->Parameters->Add(S"@Country", SqlDbType::NVarChar, 15);
  cmd->Parameters->Add(S"@City", SqlDbType::NVarChar, 15);

  da->SelectCommand = cmd;

  // Create the InsertCommand.

  cmd = new SqlCommand(S"INSERT INTO Customers (CustomerID, CompanyName) " 
                       S"VALUES (@CustomerID, @CompanyName)", conn);

  cmd->Parameters->Add(S"@CustomerID", SqlDbType::NChar, 5, S"CustomerID");
  cmd->Parameters->Add(S"@CompanyName", SqlDbType::NVarChar, 40, S"CompanyName");

  da->InsertCommand = cmd;

  return da;

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


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

See Also

SqlDataAdapter Class | SqlDataAdapter Members | System.Data.SqlClient Namespace | DeleteCommand | SelectCommand | UpdateCommand