OdbcDataAdapter.InsertCommand Property

Definition

Gets or sets an SQL statement or stored procedure used to insert new records into the data source.

C#
public System.Data.Odbc.OdbcCommand? InsertCommand { get; set; }
C#
public System.Data.Odbc.OdbcCommand InsertCommand { get; set; }

Property Value

An OdbcCommand used during an update operation to insert records in the data source that correspond to new rows in the DataSet.

Examples

The following example creates an OdbcDataAdapter and sets the SelectCommand and InsertCommand properties. It assumes that you have already created an OdbcConnection object.

C#
public static OdbcDataAdapter CreateDataAdapter(
    OdbcConnection connection)
{
    string selectCommand =
        "SELECT CustomerID, CompanyName FROM Customers";

    OdbcDataAdapter adapter = new OdbcDataAdapter(
        selectCommand, connection);
    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

    // Create the Insert, Update and Delete commands.
    adapter.InsertCommand = new OdbcCommand(
        "INSERT INTO Customers (CustomerID, CompanyName) " +
        "VALUES (?, ?)");

    adapter.UpdateCommand = new OdbcCommand(
        "UPDATE Customers SET CustomerID = ?, CompanyName = ? " +
        "WHERE CustomerID = ?");

    adapter.DeleteCommand = new OdbcCommand(
        "DELETE FROM Customers WHERE CustomerID = ?");

    // Create the parameters.
    adapter.InsertCommand.Parameters.Add("@CustomerID",
        OdbcType.Char, 5, "CustomerID");
    adapter.InsertCommand.Parameters.Add("@CompanyName",
        OdbcType.VarChar, 40, "CompanyName");

    adapter.UpdateCommand.Parameters.Add("@CustomerID",
        OdbcType.Char, 5, "CustomerID");
    adapter.UpdateCommand.Parameters.Add("@CompanyName",
        OdbcType.VarChar, 40, "CompanyName");
    adapter.UpdateCommand.Parameters.Add("@oldCustomerID",
        OdbcType.Char, 5, "CustomerID").SourceVersion =
        DataRowVersion.Original;

    adapter.DeleteCommand.Parameters.Add("@CustomerID",
        OdbcType.Char, 5, "CustomerID").SourceVersion =
        DataRowVersion.Original;

    return adapter;
}

Remarks

When the InsertCommand property is assigned to a previously created OdbcCommand object, the OdbcCommand is not cloned. Instead, InsertCommand maintains a reference to the previously created OdbcCommand.

During an update operation, if InsertCommand is not set and primary key information is present in the DataSet, you can use the OdbcCommandBuilder class to automatically generate InsertCommand, and additional commands needed to reconcile the DataSet to the data source. To do this, set the SelectCommand property of the OdbcDataAdapter. The generation logic also requires key column information to be present in the DataSet. For more information, see Generating Commands with CommandBuilders.

Poznámka

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

Applies to

Produkt Verzie
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

See also