Determing which column is identity column



I am querying the database and sticking the results into a datatable.
It would be helpful though if I could determine programmatically which
column in that datatable is the identity column (if there is one). Is
there a way to do this?

Miha Markic [MVP C#]

Hi Doug,

I guess you are using .net 1.x.
One of the options is to use OleDbConnection.GetOleDbSchemaTable. The other
option would be to use database specific ddl queries.

W.G. Ryan - MVP

In addition to Miha's comments, once the datatable is filled I think y ou
can loop through the datacolumns and check the Autoincrement property - if
it's true, that's it.


AutoIncrement doesn't work because it's a property of the dataset
itself and not the data from the database that is in the dataset.

I am using version 1.1.

I am trying to use the Data Access Block that Microsoft provides and so
in my actual app, I don't have access to the connection object (it's
open source code so I could go into it and manipulate it, but that's a
last step for me). I was just wondering if some property of the
DataSet would properly reflect that a column is a identity column.

W.G. Ryan - MVP

Doug - the adapter builds the schema for the datatable based on the db if
you don't specify it. I thought Autoincrement came back - lemme double check

I'm afraid you're going to have to modify the code though if you want to
use the GetSchemaTable or similar approach

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question