I am trying to get value of the IsUnique attribute of a database column.
It seems that the method SqlDatalReader.GetSchemaTable always
returns False, even for table defined like that:
CREATE TABLE Tab1
(
f1 INT UNIQUE,
f2 VARCHAR(50)
)
The MSDN description of the SqlDataReader.GetSchemaTable() states:
"IsUnique: true: No two rows in the base table-the table returned in
BaseTableName-can have the same value in this column. IsUnique is
guaranteed
to be true if the column constitutes a key by itself or if there is a
constraint of type UNIQUE that applies only to this column. false: The
column can contain duplicate values in the base table.The default of this
column is false."
So I have presumed that the IsUnique will be True for column f1 and False
for column f2.
Unfortunately for both of them the IsUnique value returned from
SqlDataReader.GetSchemaTable()
is False.
Below is program which creates the table Tab1, reads its schema and then
drops the table.
Regards,
Marek
=================================
Imports System.Data.SqlClient
Module Module1
Private sqlCon As New SqlConnection("data source=.;initial
catalog=Northwind;" & _
"integrated security=SSPI;")
Private sqlCmd As New SqlCommand()
Private sqlReader As SqlDataReader
Sub Main()
Dim drDataRow As DataRow
Dim dtTableWithSchema As DataTable
Call sqlCon.Open()
sqlCmd.Connection = sqlCon
sqlCmd.CommandText = "CREATE TABLE Tab1 (f1 INT UNIQUE, f2 VARCHAR
(50) )"
Call sqlCmd.ExecuteNonQuery()
sqlCmd.CommandText = "Select * From Tab1"
sqlReader = sqlCmd.ExecuteReader(CommandBehavior.SchemaOnly)
dtTableWithSchema = sqlReader.GetSchemaTable()
For Each drDataRow In dtTableWithSchema.Rows
Call Console.Write(drDataRow.Item("ColumnName"))
Call Console.Write(vbTab & vbTab)
Call Console.WriteLine(drDataRow.Item("IsUnique"))
Next
Call sqlReader.Close()
sqlCmd.CommandText = "DROP TABLE Tab1 "
Call sqlCmd.ExecuteNonQuery()
End Sub
End Module
You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.