Share via


Proprietà SqlCeDataAdapter.UpdateCommand

Ottiene o imposta un'istruzione SQL utilizzata per aggiornare i record nell'origine dati.

Spazio dei nomi  System.Data.SqlServerCe
Assembly:  System.Data.SqlServerCe (in System.Data.SqlServerCe.dll)

Sintassi

'Dichiarazione
Public Property UpdateCommand As SqlCeCommand
    Get
    Set
'Utilizzo
Dim instance As SqlCeDataAdapter
Dim value As SqlCeCommand

value = instance.UpdateCommand

instance.UpdateCommand = value
public SqlCeCommand UpdateCommand { get; set; }
public:
property SqlCeCommand^ UpdateCommand {
    SqlCeCommand^ get ();
    void set (SqlCeCommand^ value);
}
member UpdateCommand : SqlCeCommand with get, set
function get UpdateCommand () : SqlCeCommand
function set UpdateCommand (value : SqlCeCommand)

Valore proprietà

Tipo: System.Data.SqlServerCe.SqlCeCommand
Oggetto SqlCeCommand utilizzato durante l'applicazione del metodo Update per aggiornare record nell'origine dati che corrispondono alle righe modificate nell'oggetto DataSet.

Osservazioni

Durante la chiamata al metodo Update, se questa proprietà non è impostata e sono presenti informazioni sulla chiave primaria nell'oggetto DataSet, la proprietà UpdateCommand può essere generata automaticamente. Affinché questa proprietà venga generata automaticamente, impostare la proprietà SelectCommand e utilizzare l'oggetto SqlCeCommandBuilder. In seguito, qualsiasi comando aggiuntivo non impostato viene generato dalla classe SqlCeCommandBuilder. Questa logica di generazione richiede la presenza di informazioni sulle colonne di chiave primaria nell'oggetto DataSet.

Quando la proprietà UpdateCommand viene assegnata a un oggetto SqlCeCommand precedentemente creato, l'oggetto SqlCeCommand non verrà duplicato. La proprietà UpdateCommand conserva un riferimento all'oggetto SqlCeCommand creato in precedenza.

Nota

Le eventuali righe restituite dall'esecuzione del comando potrebbero essere unite all'oggetto DataSet, in base all'impostazione della proprietà UpdatedRowSource dell'oggetto SqlCeCommand.

Esempi

Nell'esempio seguente viene creato un oggetto SqlCeDataAdapter, modificato un dataset e successivamente viene chiamato il metodo Update per aggiornare l'origine dati.

Dim cmd As SqlCeCommand = Nothing
Dim adp As SqlCeDataAdapter = Nothing

Try
    adp = New SqlCeDataAdapter()
    Dim conn As New SqlCeConnection("Data Source = MyDatabase.sdf")

    ' Create the SelectCommand
    '
    cmd = conn.CreateCommand()
    cmd.CommandText = "SELECT [Employee ID], [First Name], [Last Name] FROM Employees"
    adp.SelectCommand = cmd

    ' Create the InsertCommand
    '
    cmd = conn.CreateCommand()
    cmd.CommandText = "INSERT INTO Employees ([First Name],[Last Name]) VALUES (@first, @last)"

    Dim p As SqlCeParameter = Nothing

    p = cmd.Parameters.Add("@first", SqlDbType.NVarChar, 10, "First Name")
    p.SourceVersion = DataRowVersion.Original

    p = cmd.Parameters.Add("@last", SqlDbType.NVarChar, 20, "Last Name")
    p.SourceVersion = DataRowVersion.Original

    adp.InsertCommand = cmd

    ' Create the UpdateCommand
    '
    cmd = conn.CreateCommand()
    cmd.CommandText = "UPDATE Employees SET [First Name] = @first, " + _
        "[Last Name] = @last WHERE [Employee ID] = @employeeID"

    p = cmd.Parameters.Add("@first", SqlDbType.NVarChar, 10, "First Name")
    p.SourceVersion = DataRowVersion.Current

    p = cmd.Parameters.Add("@last", SqlDbType.NVarChar, 20, "Last Name")
    p.SourceVersion = DataRowVersion.Current

    p = cmd.Parameters.Add("@employeeID", SqlDbType.NVarChar, 20, "Employee ID")
    p.SourceVersion = DataRowVersion.Original

    adp.UpdateCommand = cmd

    ' Populate the dataset with the results from the SELECT statement
    '
    Dim ds As New DataSet()
    adp.Fill(ds)

    ' Modify the dataset
    '
    MessageBox.Show("Number of rows: " & ds.Tables(0).Rows.Count)

    ' Insert some rows
    '
    ds.Tables(0).Rows.Add(New Object() {Nothing, "Barbara", "Decker"})
    ds.Tables(0).Rows.Add(New Object() {Nothing, "Joe", "Clayton"})

    ' Update some rows
    '
    ds.Tables(0).Rows(1)(1) = "David"
    ds.Tables(0).Rows(1)(2) = "Johnson"

    ' This will execute two INSERT and one UPDATE statements
    '
    adp.Update(ds.Tables(0))
Catch e As Exception
    MessageBox.Show(e.Message)
Finally
    If Not Nothing Is adp.SelectCommand Then
        adp.SelectCommand.Dispose()
    End If
    If Not Nothing Is adp.InsertCommand Then
        adp.InsertCommand.Dispose()
    End If
End Try
SqlCeCommand cmd = null;
SqlCeDataAdapter adp = null;

try
{
    adp = new SqlCeDataAdapter();
    SqlCeConnection conn = new SqlCeConnection("Data Source = MyDatabase.sdf");

    // Create the SelectCommand
    //
    cmd = conn.CreateCommand();
    cmd.CommandText = "SELECT [Employee ID], [First Name], [Last Name] FROM Employees";
    adp.SelectCommand = cmd;

    // Create the InsertCommand
    //
    cmd = conn.CreateCommand();
    cmd.CommandText = "INSERT INTO Employees ([First Name],[Last Name]) VALUES (@first, @last)";

    SqlCeParameter p = null;

    p = cmd.Parameters.Add("@first", SqlDbType.NVarChar, 10, "First Name");
    p.SourceVersion = DataRowVersion.Original;

    p = cmd.Parameters.Add("@last", SqlDbType.NVarChar, 20, "Last Name");
    p.SourceVersion = DataRowVersion.Original;

    adp.InsertCommand = cmd;

    // Create the UpdateCommand
    //
    cmd = conn.CreateCommand();
    cmd.CommandText = "UPDATE Employees SET [First Name] = @first, " +
        "[Last Name] = @last WHERE [Employee ID] = @employeeID";

    p = cmd.Parameters.Add("@first", SqlDbType.NVarChar, 10, "First Name");
    p.SourceVersion = DataRowVersion.Current;

    p = cmd.Parameters.Add("@last", SqlDbType.NVarChar, 20, "Last Name");
    p.SourceVersion = DataRowVersion.Current;

    p = cmd.Parameters.Add("@employeeID", SqlDbType.NVarChar, 20, "Employee ID");
    p.SourceVersion = DataRowVersion.Original;

    adp.UpdateCommand = cmd;

    // Populate the dataset with the results from the SELECT statement
    //
    DataSet ds = new DataSet();
    adp.Fill(ds);

    // Modify the dataset
    //
    MessageBox.Show("Number of rows: " + ds.Tables[0].Rows.Count);

    // Insert some rows
    //
    ds.Tables[0].Rows.Add(new object[] { null, "Barbara", "Decker" });
    ds.Tables[0].Rows.Add(new object[] { null, "Joe", "Clayton" });

    // Update some rows
    //
    ds.Tables[0].Rows[1][1] = "David";
    ds.Tables[0].Rows[1][2] = "Johnson";

    // This will execute two INSERT and one UPDATE statements 
    //
    adp.Update(ds.Tables[0]);


}
catch (Exception e)
{
    MessageBox.Show(e.Message);
}
finally
{
    if (null != adp.SelectCommand) adp.SelectCommand.Dispose();
    if (null != adp.InsertCommand) adp.InsertCommand.Dispose();
}

Vedere anche

Riferimento

SqlCeDataAdapter Classe

Spazio dei nomi System.Data.SqlServerCe

DeleteCommand

InsertCommand

SelectCommand