Share via


SqlCeDataAdapter.UpdateCommand Property

Obtiene o establece una instrucción SQL utilizada para actualizar registros en el origen de datos.

Espacio de nombres: System.Data.SqlServerCe
Ensamblado: System.Data.SqlServerCe (en system.data.sqlserverce.dll)

Sintaxis

'Declaración
Public Property UpdateCommand As SqlCeCommand
public SqlCeCommand UpdateCommand { get; set; }
public:
property SqlCeCommand^ UpdateCommand {
    SqlCeCommand^ get ();
    void set (SqlCeCommand^ value);
}
/** @property */
public SqlCeCommand get_UpdateCommand ()

/** @property */
public void set_UpdateCommand (SqlCeCommand value)
public function get UpdateCommand () : SqlCeCommand

public function set UpdateCommand (value : SqlCeCommand)

Valor de la propiedad

SqlCeCommand utilizado durante Update para actualizar los registros del origen de datos que se corresponden con las filas modificadas en DataSet.

Notas

Mientras opera Update, si esta propiedad no está establecida y la información de clave principal está presente en la clase DataSet, se puede generar la propiedad UpdateCommand automáticamente. Para que esta propiedad se genere automáticamente, establezca la propiedad SelectCommand y utilice la clase SqlCeCommandBuilder. Así, SqlCeCommandBuilder genera los comandos adicionales que no se hayan establecido. Esta lógica de generación requiere que haya información de las columnas de clave en DataSet.

Cuando se asigna UpdateCommand a un objeto SqlCeCommand creado previamente, la clase SqlCeCommand no se clona. La propiedad UpdateCommand mantiene una referencia al objeto SqlCeCommand creado previamente.

Nota

Si la ejecución de este comando devuelve filas, estas filas se pueden combinar con la clase DataSet dependiendo de cómo se establezca la propiedad UpdatedRowSource del objeto SqlCeCommand.

Ejemplo

El ejemplo siguiente crea un SqlCeDataAdapter, modifica un conjunto de datos y, a continuación, llama al método Update para actualizar el origen de datos.

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();
}

Seguridad para subprocesos

Todos los miembros (Compartidos en Microsoft Visual Basic) de este tipo son seguros para la ejecución de subprocesos. No se garantiza que los miembros de instancia sean seguros para subprocesos.

Plataformas

Plataformas de desarrollo

Windows Vista, Windows Mobile 5.0, Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Mobile 2003 for Pocket PC, Windows CE 5.0
Información de la versión
.NET Framework y NET Compact Framework
Se admite en 3.5
.NET Framework
Se admite en 3.0
.NET Compact Framework y .Net Framework
Se admite en 2.0

Vea también

Referencia

SqlCeDataAdapter Class
SqlCeDataAdapter Members
System.Data.SqlServerCe Namespace
SqlCeDataAdapter.DeleteCommand Property
SqlCeDataAdapter.InsertCommand Property
SqlCeDataAdapter.SelectCommand Property