ComponentOne DataObjects for .NET
Key Assigned Automatically by Database
DataObjects for .NET (Enterprise Edition) > Features and Techniques > Adding Rows and Primary Keys > Key Assigned Automatically by Database

Let's first consider the case where the new primary key value is created automatically by the database upon executing an INSERT command. It can be an autoincrement database field, or a database trigger setting the field on INSERT.

For integer primary keys with autoincrement functionality in the database, DataObjects for .NET provides a property setting, AutoIncrement = ServerAndClient that makes the whole process fully automatic. In this case, AutoIncrementSeed = -1 and AutoIncrementStep = -1, which makes temporary key values on the client negative to guarantee their uniqueness. When a row is added to the database, DataObjects for .NET uses the database capacity to retrieve the actual key value after the row is added. See IdentityColumnLastValueSelect property for explanation on how to find out if the database supports this functionality. Having retrieved the actual primary key value, DataObjects for .NET sends it back to the server as the replacement for the temporary value.

Some databases do not support automatic identity assignment (autoincrement) to database fields on adding new rows, but they support special objects (usually called sequence or generator) for generating unique identity values that are used to set identity key values in INSERT. Such databases are, for example, Oracle (object: sequence) and Interbase (object: generator). To ensure automatic identity value update on adding new rows for such databases, set the IdentityColumnLastValueSelect property to the SQL command retrieving identity value, set the field's AutoIncrementSequenceName property to the corresponding sequence (generator) object name, and set the field's IdentityColumnRetrieveMode to BeforeInsertCommand. DataObjects for .NET will obtain a new identity value, use it when inserting the new row, and refresh the identity row on the client. This is the recommended way of dealing with autoincrement keys in Oracle and in Interbase. You also have an option of creating a trigger on INSERT and setting the autoincrement key value in the trigger. This is not necessary unless you need this trigger for other purposes or already have such trigger in the database. If you do define a trigger, use IdentityColumnRetrieveMode = AfterInsertCommand with IdentityColumnLastValueSelect and AutoIncrementSequenceName to refresh the autoincremented value on the client.

If your database does not support the functionality of retrieving autoincremented (identity) value required by IdentityColumnLastValueSelect, you can still use AutoIncrement = ServerAndClient, but you need to retrieve the key value after row insert using the AfterUpdateRow event, or perform the whole row insert operation including key value retrieval in the BeforeUpdateRow event. For example, the following code using AfterUpdateRow event can be used to retrieve the actual key value after insert from a database that does not support IdentityColumnLastValueSelect:

To write code in Visual Basic

Visual Basic
Copy Code
Private Sub table_Orders_AfterUpdateRow(ByVal sender As Object, ByVal e As C1.Data.RowUpdateEventArgs) Handles table_Orders.AfterUpdateRow
    Dim connection As C1.Data.Connection
    Dim command As System.Data.OleDb.OleDbCommand
    Dim reader As System.Data.OleDb.OleDbDataReader
    connection = SchemaDef1.Schema.Connections("Connect")
    command = New System.Data.OleDb.OleDbCommand( _
        "SELECT TOP 1 OrderID FROM Orders ORDER BY OrderID DESC", _
         CType(connection.DbConnection, System.Data.OleDb.OleDbConnection))
    command.Transaction = CType(connection.DbTransaction, _
        System.Data.OleDb.OleDbTransaction)
    reader = command.ExecuteReader()
    reader.Read()
    e.Row("OrderID") = reader("OrderID")
    reader.Close()
End Sub

To write code in C#

C#
Copy Code
private void table_Orders_AfterUpdateRow(object sender, C1.Data.RowUpdateEventArgs e)
{
    C1.Data.Connection connection = schemaDef1.Schema.Connections["Connect"]);
    OleDbCommand command = new System.Data.OleDb.OleDbCommand (
        "SELECT TOP 1 OrderID FROM Orders ORDER BY OrderID DESC", (OleDbConnection)connection.DbConnection);
    command.Transaction = (System.Data.OleDb.OleDbTransaction)
        connection.DbTransaction;
    System.Data.OleDb.OleDbDataReader reader = command.ExecuteReader();
    reader.Read();
    e.Row["OrderID"] = reader["OrderID"];
    reader.Close();
}