VistaDB 6
VistaDB.Provider Namespace / VistaDBCommandBuilder Class
Members Example


In This Topic
    VistaDBCommandBuilder Class
    In This Topic
    Automatically generates single-table commands that are used to reconcile changes made to a DataSet with the associated VistaDB database.
    Syntax
    'Declaration
     
    
    <System.ComponentModel.DesignerCategoryAttribute("Component")>
    Public NotInheritable Class VistaDBCommandBuilder 
       Inherits System.Data.Common.DbCommandBuilder
    'Usage
     
    
    Dim instance As VistaDBCommandBuilder
    [System.ComponentModel.DesignerCategory("Component")]
    public sealed class VistaDBCommandBuilder : System.Data.Common.DbCommandBuilder 
    Remarks

    If you use a DataSet and are setting AutoIncrementStep and AutoIncrementSeed values you MUST set them in the right order as noted in the DataColumn.AutoIncrementSeed documentation.

    When modifying the AutoIncrement properties of a column after the table has been filled, it is important to set the AutoIncrementStep first, before the AutoIncrementSeed. This is because the effect of setting the AutoIncrementSeed changes based on the value of AutoIncrementStep.

    For example, a common pattern is to set AutoIncrementSeed and AutoIncrementStep to -1, so that there is no chance of new rows conflicting with existing rows. However, if you set the AutoIncrementSeed first, and then the AutoIncrementStep, the value of AutoIncrementSeed is ignored.

    This is the progression that illustrates the problem:

    1. The table is created during a call to DbAdapter.Fill. At this point, the AutoIncrementSeed is 0 and the AutoIncrementStep is 1.
    2. As rows are added to the table during the fill, the value of an internal variable called autoIncrementCurrent is incremented so that it is always at least 1 greater than the greatest existing value in the column.
    3. AutoIncrementSeed is set to -1. At this point, the column compares the new value of AutoIncrementSeed to the value of autoIncrementCurrent. Since autoIncrementCurrent is greater than AutoIncrementSeed, AND AutoIncrementStep is greater than 0, the column assumes that the generated values have already passed the seed value, and so it does NOT reset autoIncrementCurrent.
    4. AutoIncrementStep is set to -1.
    5. A new row is inserted. The value of the identity column is set to autoIncrementCurrent + AutoIncrementStep, which will be equal to the greatest value currently in the table, causing a constraint violation.
    6. By setting the AutoIncrementStep to -1 first, when the AutoIncrementSeed is set, the column sees that the new seed value is "farther along" (i.e. more negative) than autoIncrementCurrent, and so it resets autoIncrementCurrent to the new value of AutoIncrementSeed.

    Example
    Private Sub Example(ByVal SelectString As String, ByVal ConString As String)
        Me.mAdapter = New VistaDB.Provider.VistaDBDataAdapter
        Me.mAdapter.SelectCommand = New VistaDB.Provider.VistaDBCommand(SelectString, New VistaDBConnection(ConString))
        Dim cmdBuilder As VistaDB.Provider.VistaDBCommandBuilder = New VistaDB.Provider.VistaDBCommandBuilder(Me.mAdapter)
        Me.mAdapter.InsertCommand = cmdBuilder.GetInsertCommand
        Me.mAdapter.UpdateCommand = cmdBuilder.GetUpdateCommand
        Me.mAdapter.DeleteCommand = cmdBuilder.GetDeleteCommand
        Me.mAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
     
        ' ORDER IS VERY IMPORTANT HERE STEP THEN SEED
        mData = New DataSet
        mAdapter.Fill(mData)
        mData.Tables!Table.Columns!ID.AutoIncrement = True
        mData.Tables!Table.Columns!ID.AutoIncrementStep = -1
        mData.Tables!Table.Columns!ID.AutoIncrementSeed = -1
        mData.Tables!Table.Columns!ID.ReadOnly = False
    End Sub
    static void Example( string SelectString, string ConnString )
    {
        VistaDBDataAdapter mAdapter = new VistaDBDataAdapter();
        mAdapter.SelectCommand = new VistaDBCommand(SelectString, 
            new VistaDBConnection(ConnString));
        VistaDBCommandBuilder cmdBuilder = new VistaDBCommandBuilder(mAdapter);
     
        mAdapter.InsertCommand = cmdBuilder.GetInsertCommand();
        mAdapter.UpdateCommand = cmdBuilder.GetUpdateCommand();
        mAdapter.DeleteCommand = cmdBuilder.GetDeleteCommand();
        mAdapter.MissingSchemaAction = System.Data.MissingSchemaAction.AddWithKey;
     
        DataSet mData = new DataSet();
        mAdapter.Fill(mData);
        // NOTE: The order of these two is IMPORTANT
        // STEP MUST come before SEED
        mData.Tables[0].Columns[0].AutoIncrementStep = -1;
        mData.Tables[0].Columns[0].AutoIncrementSeed = -1;
        mData.Tables[0].Columns[0].ReadOnly = false;
    }
    Inheritance Hierarchy

    System.Object
       System.MarshalByRefObject
          System.ComponentModel.Component
             System.Data.Common.DbCommandBuilder
                VistaDB.Provider.VistaDBCommandBuilder

    Requirements

    Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

    See Also