OdbcDataAdapter.InsertCommand Proprietà

Definizione

Ottiene o imposta un'istruzione SQL o una stored procedure usata per inserire nuovi record nell'origine dati.

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

Valore della proprietà

Oggetto OdbcCommand usato durante un'operazione di aggiornamento per inserire nell'origine dati i record che corrispondono alle nuove righe di DataSet.

Esempio

Nell'esempio seguente viene creato un oggetto OdbcDataAdapter e vengono impostate le SelectCommand proprietà e InsertCommand . Presuppone che sia già stato creato un OdbcConnection oggetto .

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

Commenti

Quando la InsertCommand proprietà viene assegnata a un oggetto creato OdbcCommand in precedenza, l'oggetto OdbcCommand non viene clonato. Mantiene invece InsertCommand un riferimento all'oggetto creato OdbcCommandin precedenza.

Durante un'operazione di aggiornamento, se InsertCommand non è impostata e le informazioni sulla chiave primaria sono presenti in DataSet, è possibile usare la OdbcCommandBuilder classe per generare InsertCommandautomaticamente e comandi aggiuntivi necessari per riconciliare l'oggetto DataSet con l'origine dati. A tale scopo, impostare la SelectCommand proprietà dell'oggetto OdbcDataAdapter. La logica di generazione richiede inoltre che le informazioni sulla colonna chiave siano presenti in DataSet. Per altre informazioni, vedere Generazione dei comandi con CommandBuilders.

Nota

Se l'esecuzione di questo comando restituisce righe, è possibile aggiungere queste righe a DataSet a seconda della modalità di impostazione UpdatedRowSource della proprietà dell'oggetto OdbcCommand .

Si applica a

Vedi anche