Keys in a .NET Framework Dataset

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need a bit of clarification about keys in a .NET Framework Dataset. Keys are defined at the Dataset Level, not just within a DataTable within the DataSet. I guess that this means that keys need to be unique within the Dataset. I assume that this means that I can't have 2 keys in the same Dataset with the same name

I assume that within a single Dataset you can have 2 tables that each have a primary key with the same name, and that the keys will actually be the same. However, the uniqueness of data is determined at the level of the individual table, so that each table can have a primary key with the same value even though the primary keys are the same

Do I have this correct

Michael
 
Michael:

You can create a key on a given datacolumn by setting its .Unique Property
to true. A Dataset is merely a collection of tables, so I'm not sure what
you mean about the key at the dataset level. A Datarelation's name has to
be unique but a relation isn't a key and you can define a datarelation on
fields that aren't keys in the datatable.

The PrimaryKey analogy is the exact same as it is in a database. You can
have 10 zillion tables witha column named Whatever that all have the same
or different values. The only requirement if there are no relations defined
is that the values be unique. If relations are defined then everything in
the child table(s) must have a counterpart in the parent.

Not sure if this answers your question but if not, let me know.

HTH,

Bill
michael said:
I need a bit of clarification about keys in a .NET Framework Dataset.
Keys are defined at the Dataset Level, not just within a DataTable within
the DataSet. I guess that this means that keys need to be unique within the
Dataset. I assume that this means that I can't have 2 keys in the same
Dataset with the same name.
I assume that within a single Dataset you can have 2 tables that each have
a primary key with the same name, and that the keys will actually be the
same. However, the uniqueness of data is determined at the level of the
individual table, so that each table can have a primary key with the same
value even though the primary keys are the same.
 
Unfortunately the question is from Microsoft's ADO.NET Step by Step book(Rebecca Riorday), page 423 (Using the XML Designer). It's hard to take out of context, but the surrounding paragraphs are about creating Primary Keys. The following is reproduced without expressed permission(except for the fact that I just don't get it!)

The scope of a key is the scope of the element that contains it. In a .NET Framework DataSet schema, keys are defined at the DataSet level, which means that the key needs to be unique, not just within a DataTable, but within the DataSet as a whole

I can't find a similar statement like it in the .NET documentation. I was just looking for a little clarification
 
Well, you got me. I have the book at work and I'm half tempted to go back
there just to read it again. Rebecca knows far more than I do, but I can't
understand the context for the life of me. She posts here a lot so
hopefully she'll be able to explain. Sorry for any confusion (although
trust me, you aren't the only one confused).

michael said:
Unfortunately the question is from Microsoft's ADO.NET Step by Step
book(Rebecca Riorday), page 423 (Using the XML Designer). It's hard to take
out of context, but the surrounding paragraphs are about creating Primary
Keys. The following is reproduced without expressed permission(except for
the fact that I just don't get it!):
The scope of a key is the scope of the element that contains it. In a .NET
Framework DataSet schema, keys are defined at the DataSet level, which means
that the key needs to be unique, not just within a DataTable, but within the
DataSet as a whole.
I can't find a similar statement like it in the .NET documentation. I was
just looking for a little clarification
 
Hi Michael,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to clarify some property of
keys in ADO.NET. If there is any misunderstanding, please feel free to let
me know.

As you know, the keys are defined in the DataSet scope. Keys are defined in
the DataSet schema, including primary key and foreign key. They are mapped
to constraints in each table in the DataSet class. If you open the schema
of a typed DataSet, you will see the keys' definition.

If you have two DataTables in a DataSet, the two primary keys should have
different names in schema. As you said, the uniqueness of data is
determined at the level of the individual table. However, each primary key
defined in schema has a different name.

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Kevin:

That's where I'm getting lost, is this the case with all DataSets or only
with Typed ones?
 
Hi William,

William Ryan eMVP said:
Kevin:

That's where I'm getting lost, is this the case with all DataSets or only
with Typed ones?

I think he is talking about typed dataset schema.
 
I believe that I have the answer, I just needed to step back a bit and look at the big picture. Let's say I have 2 DataTables. Each one has its first column named "TheKey". Let's also say that I make each one a PrimaryKey. Well when you do it in SQL Server, there are actually 2 Primary Keys made, each having a different name. It doesn't matter that the name of the column that the PrimaryKey is based on is the same. The fact is that the PrimaryKeys are in fact different. When you create PrimaryKeys at the DataTable level, particularly in a designer like Access or Enterprise Manager, you won't see the name of the PrimaryKey unless you cruise through the Properties. I assume that PrimaryKeys are unique in a DataSet, the same way they are in SQL Server. And, the unique name of the PrimaryKey has nothing to do with the name of the column in the DataTable that the key is based on.
 
Hi Michael,

What you assumes are quite right. Although the primary key columns have the
same column name, the key names are different. Keys are defined in schema
in typed datasets at design time.

To William: Keys are defined in the .XSD file. We can also add unique and
foreign key constraints in code. However this time they are call the
constraints, thought I think they are the same thing.

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top