Obtain 'Identity' setting for a column in VistaDB

635 views Asked by At

I am reading the database schema for VistaDB 4.0 database using the standard ADO.NET 'DbConnection.GetSchema' API. I haven't found a way to obtain the 'Identity' setting for a column? The 'Columns' schema collection doesn't seem to have a column for this and I am not aware of any other collection that I should look into.

If it is not possible by querying any of the available collections, do I have to query some system table or view?

Any help would be appreciated.

1

There are 1 answers

0
Jason Short On BEST ANSWER

There are no "sys" tables in VistaDB. There is a [database schema] table that contains most of what you need though.

[database schema]

You can get the identity columns for a database using the database schema table like this:

select * from [database schema] where typeid = 6

Look in the help file for the typeid list and what they mean.

Then once you have the list, you can match it up to the typeid for tables to see what table the identity column came from.

The only catch with the database schema table is that you cannot self reference or join it to itself (design limitation). So if you need to pull and reference from itself you have to do it in two commands, or through a temp table. The help file has an example of how to do this as well.

Alternate Way

You can also find all the identity columns using a VistaDB stored proc:

select * from VistaDBColumnSchema() where is_identity = true

DDA

If you need to find the next value, seed, etc you can also get those through DDA (Direct Data Access) methods.

The Identities property on an IVistaDBTableSchema object is a collection of the identities for that table. That collection can then be walked to pull the individual values.

The identity information included is the Seed, Step, Tablename, and Columnname.

ADO.NET GetSchemaTable Way

And yes, there is still another way. You can call GetSchemaTable on a reader to get some more information about the underlying structure.

using (VistaDBConnection cn = new VistaDBConnection("Data Source=" + dbName))
{
    cn.Open();

    using (VistaDBCommand cmd = new VistaDBCommand("Select * from simpletable", cn))
    {
        using (VistaDBDataReader myReader = cmd.ExecuteReader(CommandBehavior.KeyInfo))
        {
            //Retrieve column schema into a DataTable.
            DataTable schemaTable = myReader.GetSchemaTable();

            foreach (DataRow myField in schemaTable.Rows)
            {
                foreach (DataColumn myProperty in schemaTable.Columns)
                {
                    System.Diagnostics.Debug.WriteLine(myProperty.ColumnName + " = " + myField[myProperty].ToString());
                }
            }
        }
    }
}