Export (0) Print
Expand All
Expand Minimize

SqlCeResultSet.CreateRecord Method

Creates a new row on the server and returns a SqlCeUpdatableRecord object.

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

public SqlCeUpdatableRecord CreateRecord()

Return Value

Type: System.Data.SqlServerCe.SqlCeUpdatableRecord
A SqlCeUpdatableRecord with the metadata of the SqlCeResultSet pre-populated.

The following example creates a new record using CreateRecord, sets values for the record, and then adds the record to the SqlCeResultSet.

SqlCeConnection conn = null;

try
{
    File.Delete("Test.sdf");

    SqlCeEngine engine = new SqlCeEngine("Data Source = Test.sdf");
    engine.CreateDatabase();

    conn = new SqlCeConnection("Data Source = Test.sdf");
    conn.Open();

    SqlCeCommand cmd = conn.CreateCommand();
    cmd.CommandText = "CREATE TABLE myTable (col1 INT, col2 MONEY, col3 NVARCHAR(200))";
    cmd.ExecuteNonQuery();

    // For optimal performance use base table access
    //
    cmd.CommandText = "myTable";
    cmd.CommandType = CommandType.TableDirect;

    SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Updatable | 
        ResultSetOptions.Scrollable);

    SqlCeUpdatableRecord rec = rs.CreateRecord();

    // Insert two records
    //
    rec.SetInt32(0, 34);
    rec.SetDecimal(1, (decimal)44.66);
    rec.SetString(2, "Sample text 1");

    rs.Insert(rec);

    rec.SetInt32(0, 35);
    rec.SetDecimal(1, (decimal)94.25);
    rec.SetString(2, "Sample text 2");

    rs.Insert(rec);
}
catch (Exception e)
{
    MessageBox.Show(e.Message);
}
finally
{
    conn.Close();
}


Show:
© 2014 Microsoft