none
IsUnique field always false in GetSchemaTable() RRS feed

  • Question

  • Hi,

     

    I'm trying to get data table info from GetSchemaTable() method. Then I iterate trough fields properties and IsUnique property is always false, but in the database table is field set as unique.

     

    Here is a code block:

     

    Code Block

    ...

    cmd.Connection = connection;

    cmd.CommandText = "SELECT * FROM " + tableName;

    IDataReader myReader = cmd.ExecuteReader(CommandBehavior.KeyInfo | CommandBehavior.SchemaOnly);

    schemaTable = myReader.GetSchemaTable();

    ...

    foreach (DataRow row in schemaTable.Rows)

    {

    ...

    bool isUnique =  (bool) row["IsUnique"];

    }

     

    ...

     

     

    Can you advise me what is wrong?

     

    Thanks

     

    Wednesday, December 12, 2007 2:12 PM

Answers

All replies

  •  

    do you really need the KeyInfo behaviour? Just a wild guess but I would say if you only need the table structure just cancel the keyinfo behaviour and the problem should be solved...
    Wednesday, December 12, 2007 4:01 PM
  • Thanks for answer.
    I removed KeyInfo, but the same result. IsUnique is always false.
    Wednesday, December 12, 2007 11:08 PM
  • The IsUnique is only true for columns with TimeStamp enabled in the SQL Server database, there can only be one column with TimeStamp enabled per table.  Try the link below for the FCL(framework class library) docs.

     

    http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getschematable.aspx

    Thursday, December 13, 2007 12:26 AM
  • Thanks for answer. You are right about the IsUnique,

    but

    Is exists any way how to get information from table schema if column is Unique? I have columns in the tables which has unique constraint and I need to know which columns are Unique. I tried also IsKey property, but this also always return false. Isn't it strange behaviour?

    thanks

    bye
    Thursday, December 13, 2007 11:20 AM
  • That is because you need TimeStamp with the Unique constraint to use that property so read the docs again.

     

    Thursday, December 13, 2007 2:40 PM