Add primary key

K

Konrad

Hi

How to add primary key column to DataTable which
have no primary key? Using if possible ADO.NET.
It concerns Access database.

Thanks
Konrad
 
P

Prateek

Hi,

Use the PrimaryKey propperty of the DataTable object:

1. Single column as the primary key:

workTable.PrimaryKey = New DataColumn() {workTable.Columns("CustID")}

2. Two columns as a primary key:

workTable.PrimaryKey = New DataColumn() {workTable.Columns("CustLName"),
workTable.Columns("CustFName")}


-Prateek


Hi

How to add primary key column to DataTable which
have no primary key? Using if possible ADO.NET.
It concerns Access database.

Thanks
Konrad
 
K

Konrad

And how next write such a columns to database.
When previously the primary key was absent
the update command is not builded by
CommandBuilder?

Konrad
 
W

William Ryan

Konrad:

If your table isn't keyed, niether the DataAdapter Configuration wizard or
the CommandBuilder is going to be successful generating update logic. You
can run selects off of an unkeyed table, but that's about it. The Key that
you create client side will enforce integrity, but the back end doesn' tknow
about it.

Is the Non-Key thing on the backend a requirement? Stated humbly, there
probably aren't too many instances where not having a key is value
added.......and there are many shops where having a Key on every table is a
requirement. It'll definitely make your life easier.

Nonetheless, if you don't have a key and can't put one on it, you'll need to
roll out your own update logic which is doable, but can be a little work
depending on your table.

Cheers,

Bill
 
S

Steven Bras [MS]

Great response, Bill. Thanks for posting it!

Steven Bras, MCSD
Microsoft Developer Support/Data Access Technologies

This posting is provided "AS IS" with no warranties, and confers no rights.

Microsoft Security Announcement: Have you installed the patch for Microsoft
Security Bulletin MS03-026?  If not Microsoft strongly advises you to
review the information at the following link regarding Microsoft Security
Bulletin MS03-026
http://www.microsoft.com/security/security_bulletins/ms03-026.asp and/or to
visit Windows Update at http://windowsupdate.microsoft.com to install the
patch. Running the SCAN program from the Windows Update site will help to
insure you are current with all security patches, not just MS03-026.
 

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