SqlCeCommandBuilder.GetInsertCommand Method

Gets the automatically generated SqlCeCommand object required to perform inserts on the database when an application calls Update on the SqlCeDataAdapter.

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

Syntax

'Declaration
Public Function GetInsertCommand As SqlCeCommand
'Usage
Dim instance As SqlCeCommandBuilder
Dim returnValue As SqlCeCommand

returnValue = instance.GetInsertCommand()
public SqlCeCommand GetInsertCommand()
public:
SqlCeCommand^ GetInsertCommand()
member GetInsertCommand : unit -> SqlCeCommand 
public function GetInsertCommand() : SqlCeCommand

Return Value

Type: System.Data.SqlServerCe.SqlCeCommand
The automatically generated SqlCeCommand object required to perform inserts.

Remarks

An application can use the GetInsertCommand method for informational or troubleshooting purposes because it returns the SqlCeCommand object to be executed.

You can also use GetInsertCommand as the basis of a modified command. For example, you might call GetInsertCommand and modify one of its properties, and then explicitly set that on the SqlCeDataAdapter.

After the SQL statement is first generated, the application must explicitly call RefreshSchema() if the application changes the statement in any way. Otherwise, the GetInsertCommand still uses information from the previous statement, and that information might not be correct. The SQL statements are first generated when the application calls either Update or GetInsertCommand.

Examples

The following example shows calling the GetInsertCommand method of the SqlCeCommandBuilder.

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

    Dim cmd As SqlCeCommand = conn.CreateCommand()
    cmd.CommandText = "SELECT * FROM employees"

    Dim adp As New SqlCeDataAdapter(cmd)

    Dim cb As New SqlCeCommandBuilder()
    cb.DataAdapter = adp

    MessageBox.Show(cb.GetUpdateCommand().CommandText)
    MessageBox.Show(cb.GetInsertCommand().CommandText)
    MessageBox.Show(cb.GetDeleteCommand().CommandText)

    Dim ds As New DataSet("test")
    adp.Fill(ds)

    ' Modify the contents of the DataSet
    '
    ds.Tables(0).Rows(0)("First Name") = "Joe"

    adp.Update(ds)

Catch e1 As Exception
    Console.WriteLine(e1.ToString())
End Try
try
{
    SqlCeConnection conn = new SqlCeConnection("Data Source = MyDatabase.sdf");
    conn.Open();

    SqlCeCommand cmd = conn.CreateCommand();
    cmd.CommandText = "SELECT * FROM employees";

    SqlCeDataAdapter adp = new SqlCeDataAdapter(cmd);

    SqlCeCommandBuilder cb = new SqlCeCommandBuilder();
    cb.DataAdapter = adp;

    MessageBox.Show(cb.GetUpdateCommand().CommandText);
    MessageBox.Show(cb.GetInsertCommand().CommandText);
    MessageBox.Show(cb.GetDeleteCommand().CommandText);

    DataSet ds = new DataSet("test");
    adp.Fill(ds);

    // Modify the contents of the DataSet
    //
    ds.Tables[0].Rows[0]["First Name"] = "Joe";

    adp.Update(ds);

}
catch (Exception e1)
{
    Console.WriteLine(e1.ToString());
}

See Also

Reference

SqlCeCommandBuilder Class

GetInsertCommand Overload

System.Data.SqlServerCe Namespace