Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

SqlCeDataAdapter.InsertCommand Property

Gets or sets an SQL statement used to insert new records into the data source.

Namespace: System.Data.SqlServerCe
Assembly: System.Data.SqlServerCe (in system.data.sqlserverce.dll)

public SqlCeCommand InsertCommand { get; set; }
/** @property */
public SqlCeCommand get_InsertCommand ()

/** @property */
public void set_InsertCommand (SqlCeCommand value)

public function get InsertCommand () : SqlCeCommand

public function set InsertCommand (value : SqlCeCommand)

Not applicable.

Property Value

A SqlCeCommand used during Update to insert records in the data source that correspond to new rows in the DataSet.

During a call to Update, if this property is not set and primary key information is present in the DataSet, the InsertCommand can be generated automatically if you 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 InsertCommand is assigned to a previously-created SqlCeCommand, the SqlCeCommand is not cloned. The InsertCommand maintains a reference to the previously-created SqlCeCommand object.


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

The following example creates a SqlCeDataAdapter and sets some of its properties.

SqlCeCommand cmd = null;
SqlCeDataAdapter adp = null;

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

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

    // Insert some rows
    ds.Tables[0].Rows.Add(new object[] { null, "Nancy", "Smith" });
    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 

catch (Exception e)
    if (null != adp.SelectCommand) adp.SelectCommand.Dispose();
    if (null != adp.InsertCommand) adp.InsertCommand.Dispose();

Windows CE, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows XP Professional x64 Edition, Windows XP SP2

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

© 2015 Microsoft