Sunday, March 25, 2012

determine if autoincrement is set in a table, form asp.net

I want to access sqlserver table properties from asp.net .

How do i know that a table defined in sqlserver has autoincrement field or not.

Actually i have to access all the tables in a database and execute different function for table with auto increment on and off.

I am not being able identify this property from codes.
Please help.
thank you

Hey,

When you connect to the database, you can't know that from ADO.NET; you have to recreate identities manually on the ADO.NET side. However, if you have SQL Server 2005 API's installed, you can use the SMO objects to connect to a 7.0, 2000, or 2005 database and find this information out.

|||Thanks brian

I am using sql 2000 and SqlDataAdapter object

I think the following code should work but its not working.
any ideas

Dim dsAs DataSet ' = ( got dataset from a table)
Dim hasAutoIncrementAsBoolean =FalseForEach columnAs DataColumnIn ds.Tables(0).Columns If column.AutoIncrementThen

hasAutoIncrement =

True
ExitFor EndIfNext|||

Hey,

The problem is there isn't any autoincrement returned back from the dataset; you have to establish that programatically. So you need to do:

dataSet.Tables(0).Columns(0).AutoIncrement = true
dataSet.Tables(0).Columns(0).AutoIncrementStep = 1

Etc. You have to set this up by yourself; it doesn't pull it back for you.

|||
SELECT *,COLUMNPROPERTY(OBJECT_ID(TABLE_NAME),COLUMN_NAME,'IsIdentity')AS IS_IDENTITYFROM INFORMATION_SCHEMA.COLUMNS
|||

Which would lead you to this query:

SELECT COUNT(*)

FROM (

SELECT

*,COLUMNPROPERTY(OBJECT_ID(TABLE_NAME),COLUMN_NAME,'IsIdentity')AS IS_IDENTITY

FROM

INFORMATION_SCHEMA.COLUMNS

) t1

WHERETABLE_NAME=@.MyTable AND IS_IDENITITY=1

|||Thank you very muchMotley

exactly what i was looking for.

Thanks

No comments:

Post a Comment