Home All Groups Group Topic Archive Search About

Multicolumn UniqueConstraint in typed dataset/datatable - How to implement?

Author
23 Jun 2006 5:08 PM
Marcus
I am trying to create a unique constraint for a typed datatable for
multiple columns so the same group of values will not appear more than
once for any one row (I can't make this group of columns a composite
primary key as one of the columns can sometimes be null). If I was
creating a non-typed dataset, then in the code it is quite simple to
implement this:

Dim dt As New DataTable("MyTable")

-- CODE TO DEFINE TABLE HERE ---

Dim myUniqueConstraint As UniqueConstraint
myUniqueConstraint = New UniqueConstraint(New DataColumn()
{dt.Columns("column1"), dt.Columns("column2"), dt.Columns("column3"),
dt.Columns("column4")})
dt.Constraints.Add(myUniqueConstraint)

However, I don't see how to do this for a typed dataset. I can select
all of the columns that I want in the dataset designer for the
datatable and assign the "unique" property to true, but the code that
it generates is individual unique constraints for each of the columns,
not a composite for all columns. Is there a way to set this in the
designer? Perhaps I can add the code in the partial class of the
dataset (I don't know how to do this...suggestions?). Is this possible
at all, or do I have to change my typed dataset, which only contains
one table, back into an untyped dataset?

Thanks,
Marcus

Author
27 Jun 2006 5:49 PM
Marcus
Anybody? This seems to me to not be an unusual desire, to make a
multi-column Unique constraint on a typed dataset. Perhaps I am missing
something...

M.


Marcus wrote:
Show quote
> I am trying to create a unique constraint for a typed datatable for
> multiple columns so the same group of values will not appear more than
> once for any one row (I can't make this group of columns a composite
> primary key as one of the columns can sometimes be null). If I was
> creating a non-typed dataset, then in the code it is quite simple to
> implement this:
>
> Dim dt As New DataTable("MyTable")
>
>  -- CODE TO DEFINE TABLE HERE ---
>
> Dim myUniqueConstraint As UniqueConstraint
> myUniqueConstraint = New UniqueConstraint(New DataColumn()
> {dt.Columns("column1"), dt.Columns("column2"), dt.Columns("column3"),
> dt.Columns("column4")})
> dt.Constraints.Add(myUniqueConstraint)
>
> However, I don't see how to do this for a typed dataset. I can select
> all of the columns that I want in the dataset designer for the
> datatable and assign the "unique" property to true, but the code that
> it generates is individual unique constraints for each of the columns,
> not a composite for all columns. Is there a way to set this in the
> designer? Perhaps I can add the code in the partial class of the
> dataset (I don't know how to do this...suggestions?). Is this possible
> at all, or do I have to change my typed dataset, which only contains
> one table, back into an untyped dataset?
>
> Thanks,
> Marcus

AddThis Social Bookmark Button