DataTable.PrimaryKey Can I change the values of a primary key?

N

NordeDakota

Scenario: using .NET DataTable and reading an existing record from MS
SQL Database using Ado.Net, and having .Net and SQL define the primary
key using FillSchema.

When I alter or change the values and I update or AcceptChanges my
record is modified as expected in the database. What I'm wondering and
I have not tested, yet, if I load an existing record from the data
source into a DataTable and the PrimaryKey is defaulted... should .Net
stop me or throw an exception if I try to alter a primary key value?

If I load a customer record into my DataTable with CustomerID as the
PrimaryKey and from the load this value is 1234; I think that the
DataTable should throw an exception if I attempt to change 1234 to
9987 or something else. However, I suspsect that .Net will allow me to
change a primary key value.
 
D

David Ching

NordeDakota said:
Scenario: using .NET DataTable and reading an existing record from MS
SQL Database using Ado.Net, and having .Net and SQL define the primary
key using FillSchema.

When I alter or change the values and I update or AcceptChanges my
record is modified as expected in the database. What I'm wondering and
I have not tested, yet, if I load an existing record from the data
source into a DataTable and the PrimaryKey is defaulted... should .Net
stop me or throw an exception if I try to alter a primary key value?

If I load a customer record into my DataTable with CustomerID as the
PrimaryKey and from the load this value is 1234; I think that the
DataTable should throw an exception if I attempt to change 1234 to
9987 or something else. However, I suspsect that .Net will allow me to
change a primary key value.

I might very well be wrong, but my understanding of a Primary Key is that
all rows in the table have a UNIQUE value for it. It doesn't mean you can't
change it. If you try to change it to a value already present in another
row, perhaps that is disallowed. I guess it's easy enough to try, though.
Let us know what happens!

-- David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top