DataTable Question

  • Thread starter Thread starter Roshawn
  • Start date Start date
R

Roshawn

Hi,

I have a DataSet that contains one DataTable and holds data pertaining to
books. This table has a primary key column based on the ISBN of books and
is loaded with data. Suppose I add other books to the table and one of them
is a book that is already in the table. Will the table now have two rows
for the same book, or will the new row just be discarded? Also, what type
of errors are thrown, if any?

Thanks,
Roshawn
 
You can't add duplicate keys (in your case, ISBNs) and yes, SQL server will
throw an exception. Not a serious one, just handle it and it will be ok, and
the dup ID will not be added.
 
Exceptions are expensive. I wouldn't just handle it and move on. If you
are populating your DataSet from SQL with the ISBN's as the primary key,
then you can simply check the DataSet for an existing row with the ISBN of
the new proposed row and only put it into the DataSet if it isn't already
there.
 
Roshawn:

The key's job is to Ensure that it throws an exception and doens't allow dup
values in there. You may want to check the existence of the value first
with a IF Exists(SELECT ...) Begin ---Update Logic here
Else
--Insert

End

--
W.G. Ryan MVP Windows - Embedded

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/community/newsgroups
 
Thanks to everyone for their response.

Regarding the duplicate primary key, I would like to examine the DataTable
for duplicate keys before I submit the update to the SQL database. How
would I write code to accomplish this task using ADO.NET instead of relying
on the SQL database to handle it?

Roshawn
 
Back
Top