OdbcDataAdapter.InsertCommand Propiedad

Definición

Obtiene o establece una instrucción SQL o procedimiento almacenado utilizado para insertar nuevos registros en el origen de datos.Gets or sets an SQL statement or stored procedure used to insert new records into the data source.

public:
 property System::Data::Odbc::OdbcCommand ^ InsertCommand { System::Data::Odbc::OdbcCommand ^ get(); void set(System::Data::Odbc::OdbcCommand ^ value); };
public System.Data.Odbc.OdbcCommand InsertCommand { get; set; }
public System.Data.Odbc.OdbcCommand? InsertCommand { get; set; }
member this.InsertCommand : System.Data.Odbc.OdbcCommand with get, set
Public Property InsertCommand As OdbcCommand

Valor de propiedad

OdbcCommand

OdbcCommand utilizado durante una operación de actualización para insertar registros en el origen de datos que corresponden a las nuevas filas en el DataSet.An OdbcCommand used during an update operation to insert records in the data source that correspond to new rows in the DataSet.

Ejemplos

En el ejemplo siguiente se crea un OdbcDataAdapter y se establecen las SelectCommand InsertCommand propiedades y.The following example creates an OdbcDataAdapter and sets the SelectCommand and InsertCommand properties. Se supone que ya ha creado un OdbcConnection objeto.It assumes that you have already created an OdbcConnection object.

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;
}
Public Function CreateDataAdapter( _
    ByVal connection As OdbcConnection) As OdbcDataAdapter

    Dim selectCommand As String = _
        "SELECT CustomerID, CompanyName FROM Customers"
    Dim adapter As OdbcDataAdapter = _
        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
End Function

Comentarios

Cuando la InsertCommand propiedad se asigna a un objeto creado previamente OdbcCommand , OdbcCommand no se clona.When the InsertCommand property is assigned to a previously created OdbcCommand object, the OdbcCommand is not cloned. En su lugar, InsertCommand mantiene una referencia a la creada anteriormente OdbcCommand .Instead, InsertCommand maintains a reference to the previously created OdbcCommand.

Durante una operación de actualización, si InsertCommand no se establece y la información de clave principal está presente en DataSet , puede utilizar la OdbcCommandBuilder clase para generar automáticamente los InsertCommand comandos adicionales necesarios para reconciliar el DataSet en el origen de datos.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. Para ello, establezca la SelectCommand propiedad de OdbcDataAdapter .To do this, set the SelectCommand property of the OdbcDataAdapter. La lógica de generación también requiere que la información de la columna de clave esté presente en DataSet .The generation logic also requires key column information to be present in the DataSet. Para obtener más información, vea Generar comandos con objetos CommandBuilder.For more information, see Generating Commands with CommandBuilders.

Nota

Si la ejecución de este comando devuelve filas, estas filas se pueden agregar al DataSet dependiendo de cómo se establezca la UpdatedRowSource propiedad del OdbcCommand objeto.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.

Se aplica a