Export (0) Print
Expand All

SqlCeDataAdapter.UpdateCommand Property

Gets or sets an SQL statement used to update records in the data source.

Namespace:  System.Data.SqlServerCe
Assembly:  System.Data.SqlServerCe (in System.Data.SqlServerCe.dll)

public SqlCeCommand UpdateCommand { get; set; }

Property Value

Type: System.Data.SqlServerCe.SqlCeCommand
A SqlCeCommand used during Update to update records in the data source that correspond to modified rows in the DataSet.

During Update, if this property is not set and primary key information is present in the DataSet, the UpdateCommand can be generated automatically. To have this property generated automatically, set the SelectCommand property and use the SqlCeCommandBuilder. Then, any additional commands that you do not set are generated by the SqlCeCommandBuilder. This generation logic requires key column information to be present in the DataSet.

When UpdateCommand is assigned to a previously-created SqlCeCommand, the SqlCeCommand is not cloned. The UpdateCommand maintains a reference to the previously-created SqlCeCommand object.

NoteNote

If execution of this command returns rows, these rows may be merged with the DataSet, depending on how you set the UpdatedRowSource property of the SqlCeCommand object.

The following example creates a SqlCeDataAdapter, modifies a dataset, and then calls the Update method to update the data source.

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


Show:
© 2014 Microsoft