SqlDataAdapter.InsertCommand Property


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

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

member InsertCommand : SqlCommand with get, set

Property Value

Type: System.Data.SqlClient.SqlCommand

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 Generating Commands with CommandBuilders.

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.

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

For every column that you propagate to the data source on Update, a parameter should be added to InsertCommand, UpdateCommand, or DeleteCommand. The SourceColumn property of the parameter should be set to the name of the column. This indicates that the value of the parameter is not set manually, but is taken from the particular column in the currently processed row.

The following example creates a SqlDataAdapter and sets the SelectCommand, InsertCommand, UpdateCommand, and DeleteCommand properties. It assumes you have already created a SqlConnection object.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 1.1
Return to top