PC Review


Reply
Thread Tools Rate Thread

DataColumn's Unique property (applies also to null values?)

 
 
=?Utf-8?B?R3V5?=
Guest
Posts: n/a
 
      17th Sep 2004
Under SQL Server 2000, a column can be defined as null and unique at the same
time. Under these circumstances, when a new row is added, uniqueness is
ensured for only non null values.

Under ADO.Net, I would like to know whether the datacolumn's unique property
mirrors that of SQL server 2000. I am trying to create a datacolumn that can
contain DBNull.Value but is unique when its values are not DBNull.Value. My
experience with the DataColumn.Unique Property is that it will check for
uniqueness regardless of whether AllowDBNull is true or not, ie if you cannot
have several DataRows with null value in this column if the DataColumn's
Unique property is set to true.

Can anyone confirm this?
 
Reply With Quote
 
 
 
 
W.G. Ryan eMVP
Guest
Posts: n/a
 
      17th Sep 2004
That's correct:

DataTable dt = new DataTable();

DataColumn dc = new DataColumn("Blah",
System.Type.GetType("System.String"));

dc.Unique = true;

dc.AllowDBNull = true;

dt.Columns.Add(dc);

for(int i =0;i<5;i++)

{

DataRow dro = dt.NewRow();

dt.Rows.Add(dro);

}



This will blow up on the second pass as it stands. You may want to use some
other column and use a DefaultValue to get you to the same place.



HTH,



Bill

"Guy" <(E-Mail Removed)> wrote in message
news:2310AF35-DD75-4A06-9E92-(E-Mail Removed)...
> Under SQL Server 2000, a column can be defined as null and unique at the

same
> time. Under these circumstances, when a new row is added, uniqueness is
> ensured for only non null values.
>
> Under ADO.Net, I would like to know whether the datacolumn's unique

property
> mirrors that of SQL server 2000. I am trying to create a datacolumn that

can
> contain DBNull.Value but is unique when its values are not DBNull.Value.

My
> experience with the DataColumn.Unique Property is that it will check for
> uniqueness regardless of whether AllowDBNull is true or not, ie if you

cannot
> have several DataRows with null value in this column if the DataColumn's
> Unique property is set to true.
>
> Can anyone confirm this?



 
Reply With Quote
 
=?Utf-8?B?R3V5?=
Guest
Posts: n/a
 
      20th Sep 2004
Thank you for the reply.

What I don't understand is why doesn't ADO.NET mirror the SQL server?
Having two definitions of uniqueness is highly misleading.

I can't see how I can implement the SQL server type of column uniqueness (ie
uniqueness only on non null values). Can anyone give more detailed pointers?

Regards,

Guy


"W.G. Ryan eMVP" wrote:

> That's correct:
>
> DataTable dt = new DataTable();
>
> DataColumn dc = new DataColumn("Blah",
> System.Type.GetType("System.String"));
>
> dc.Unique = true;
>
> dc.AllowDBNull = true;
>
> dt.Columns.Add(dc);
>
> for(int i =0;i<5;i++)
>
> {
>
> DataRow dro = dt.NewRow();
>
> dt.Rows.Add(dro);
>
> }
>
>
>
> This will blow up on the second pass as it stands. You may want to use some
> other column and use a DefaultValue to get you to the same place.
>
>
>
> HTH,
>
>
>
> Bill
>
> "Guy" <(E-Mail Removed)> wrote in message
> news:2310AF35-DD75-4A06-9E92-(E-Mail Removed)...
> > Under SQL Server 2000, a column can be defined as null and unique at the

> same
> > time. Under these circumstances, when a new row is added, uniqueness is
> > ensured for only non null values.
> >
> > Under ADO.Net, I would like to know whether the datacolumn's unique

> property
> > mirrors that of SQL server 2000. I am trying to create a datacolumn that

> can
> > contain DBNull.Value but is unique when its values are not DBNull.Value.

> My
> > experience with the DataColumn.Unique Property is that it will check for
> > uniqueness regardless of whether AllowDBNull is true or not, ie if you

> cannot
> > have several DataRows with null value in this column if the DataColumn's
> > Unique property is set to true.
> >
> > Can anyone confirm this?

>
>
>

 
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Count unique values in one column if values in corresponding columnare null? allie357 Microsoft Excel Programming 1 7th Dec 2008 09:37 AM
Null values in a DataColumn.PrimaryKey column ScottSto Microsoft ADO .NET 3 11th Oct 2005 07:21 PM
Is it possible to set a unique constraint on a dataset column but still allow null values? Tim Frawley Microsoft VB .NET 4 22nd Mar 2004 01:04 AM
Null Property Values Mike Shilts Microsoft Word Document Management 1 21st Feb 2004 01:36 AM
Unique Constraint with Null values in FK Tom Lewis Microsoft Access ADP SQL Server 0 15th Jul 2003 09:24 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:10 PM.