PC Review Forums Newsgroups Microsoft DotNet Microsoft ADO .NET Multicolumn UniqueConstraint in typed dataset/datatable - How to implement?

Reply

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

 
Thread Tools Rate Thread
Old 23-06-2006, 06:08 PM   #1
Marcus
Guest
 
Posts: n/a
Default Multicolumn UniqueConstraint in typed dataset/datatable - How to implement?


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

  Reply With Quote
Old 27-06-2006, 06:49 PM   #2
Marcus
Guest
 
Posts: n/a
Default Re: Multicolumn UniqueConstraint in typed dataset/datatable - How to implement?

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:
> 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


  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off