Partager via


OleDbDataAdapter.InsertCommand Propriété

Définition

Obtient ou définit une instruction SQL ou une procédure stockée utilisée pour insérer de nouveaux enregistrements dans la source de données.

public:
 property System::Data::OleDb::OleDbCommand ^ InsertCommand { System::Data::OleDb::OleDbCommand ^ get(); void set(System::Data::OleDb::OleDbCommand ^ value); };
public System.Data.OleDb.OleDbCommand? InsertCommand { get; set; }
[System.Data.DataSysDescription("DbDataAdapter_InsertCommand")]
public System.Data.OleDb.OleDbCommand InsertCommand { get; set; }
public System.Data.OleDb.OleDbCommand InsertCommand { get; set; }
member this.InsertCommand : System.Data.OleDb.OleDbCommand with get, set
[<System.Data.DataSysDescription("DbDataAdapter_InsertCommand")>]
member this.InsertCommand : System.Data.OleDb.OleDbCommand with get, set
Public Property InsertCommand As OleDbCommand

Valeur de propriété

Utilisé OleDbCommand pendant Update(DataSet) l’insertion d’enregistrements dans la source de données qui correspondent à de nouvelles lignes dans le DataSet.

Attributs

Exemples

L’exemple suivant crée un OleDbDataAdapter et définit les propriétés et InsertCommand les SelectCommand propriétés. Il part du principe que vous avez déjà créé un OleDbConnection objet.

public static OleDbDataAdapter CreateCustomerAdapter(
    OleDbConnection connection)
{
    OleDbDataAdapter adapter = new OleDbDataAdapter();
    OleDbCommand command;

    // Create the SelectCommand.
    command = new OleDbCommand("SELECT CustomerID FROM Customers " +
        "WHERE Country = ? AND City = ?", connection);

    command.Parameters.Add("Country", OleDbType.VarChar, 15);
    command.Parameters.Add("City", OleDbType.VarChar, 15);

    adapter.SelectCommand = command;

    // Create the InsertCommand.
    command = new OleDbCommand(
        "INSERT INTO Customers (CustomerID, CompanyName) " +
        "VALUES (?, ?)", connection);

    command.Parameters.Add(
        "CustomerID", OleDbType.Char, 5, "CustomerID");
    command.Parameters.Add(
        "CompanyName", OleDbType.VarChar, 40, "CompanyName");

    adapter.InsertCommand = command;
    return adapter;
}
Public Shared Function CreateCustomerAdapter( _
    connection As OleDbConnection) As OleDbDataAdapter 
  
    Dim adapter As New OleDbDataAdapter()
    Dim command As OleDbCommand

    ' Create the SelectCommand.
    command = New OleDbCommand("SELECT CustomerID FROM Customers " & _
        "WHERE Country = ? AND City = ?", connection)

    command.Parameters.Add("Country", OleDbType.VarChar, 15)
    command.Parameters.Add("City", OleDbType.VarChar, 15)

    adapter.SelectCommand = command

    ' Create the InsertCommand.
    command = New OleDbCommand( _
        "INSERT INTO Customers (CustomerID, CompanyName) " & _
        "VALUES (?, ?)", connection)

    command.Parameters.Add( _
        "CustomerID", OleDbType.Char, 5, "CustomerID")
    command.Parameters.Add( _
        "CompanyName", OleDbType.VarChar, 40, "CompanyName")

    adapter.InsertCommand = command
    Return adapter
End Function

Remarques

Pendant Update, si cette propriété n’est pas définie et que les informations de clé primaire sont présentes dans le DataSet, elles InsertCommand peuvent être générées automatiquement si vous définissez la SelectCommand propriété et utilisez le OleDbCommandBuilder. Ensuite, toutes les commandes supplémentaires que vous ne définissez pas sont générées par le OleDbCommandBuilder. Cette logique de génération nécessite que les informations de colonne clés soient présentes dans le DataSet. Pour plus d’informations, consultez Génération de commandes avec CommandBuilders.

Lorsqu’il InsertCommand est affecté à un précédemment créé OleDbCommand, il OleDbCommand n’est pas cloné. Conserve InsertCommand une référence à l’objet créé OleDbCommand précédemment.

Note

Si l’exécution de cette commande retourne des lignes, ces lignes peuvent être ajoutées à la DataSet façon dont vous définissez la UpdatedRowSource propriété de l’objet OleDbCommand .

S’applique à

Voir aussi