Solved

Refreshing DataSet w/ Dbase's Primary Keys

Posted on 2003-10-27
6
445 Views
Last Modified: 2010-04-16
I have a number of records to add to the database so Im using a Dataset to compose the table and then calling the adapter's Update(myDataSet, "myTable") to refresh the data source with the new records.  The problem is with the autonumber primary key the database automatically generates for each new record added to it.  I need this unqiue value for each row of my dataset because I'm creating another table which requires it.

I've been using the adapter's Fill(myDataSet, "myTable") in hopes of refreshing the dataset's primary key field with the proper values from the datasource but with no luck.  However, calling with Fill(myDataSet, "myNewTable"), where myNewTable is just a new table name within the same dataset, does retrieve the actual primary keys from the data source.  The problem I have with the second method is it retrieves ALL the records from the data source, including the newly added items) instead of just the new records, and more importantly the specific coulmn, that arent within the dataset.  

I have no idea what the adapter's Update() command does underneath but using a dataset for all my new records and updating the datasource as one block seems like a more efficient method than calling the INSERT sql command, .ExecuteNonQuery(), then SELECT @@IDENTITY for each record.  But how exactly do i retrieve the actual datasource primary keys for each record within the dataset that was added?  ...Without having to retrieve the entire datasource again?

Hope this makes sense.
0
Comment
Question by:aniston
  • 3
  • 2
6 Comments
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9632122
Normally the DataSets are "clever" and update/insert only changed/new records. I think they also call select @@identity only for those records, i.e. not for all records. So the data that is not changed is also not sent to the server and not re-retrieved. Based on this I think using the implemented functionality of exactly calling SELECT @@IDENTITY for each record (changed or new) is the most effective one you can have.
0
 

Author Comment

by:aniston
ID: 9632166
I've created an empty database in Access 2000 consisting of 3 tables:

(Table A)
Field 1 = "aID" (Primary key, autoincrement)
Field 2 = "Name" (Text)

(Table B)
Field 1 = "bID" (Primary Key, autoincrement)
Field 2 = "Song" (Text)

(Table C)
Field 1 = "aID" (Foreign key from Table A)
Field 2 = "bID" (Foreign key from Table B)

The relationship amongst the tables is one to many from Table A to Table C, and one to many from Table B to Table C.  I've composed a list of Names to be inserted into Table A and a list of Songs to be added to Table B.  Table C serves as the middle man allowing me to identify what names match with what songs.

Adding new Names to Table A and new Songs to Table B have been easy using a DataSet, DataAdapter, and calling Fill() and Update().  However adding new records to Table C have been difficult.  I'm unable to provide the actual Foreign Key values it requires.  The primary key values in these other tables are determined "after" i add the record to the data source and so if I want to fill Table C i would need to retrieve the entire contents of Table A and B after adding the new records, step thru them for the primary keys and then set Table C.  It all seems very crude.  

Is there a better/correct method for inserting records into my database layout?  Perhaps DataSets arent the answer?
0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 50 total points
ID: 9632193
The answer is only one: what you see as "crude" is what is always done and what is the correct way to do it. I suggest the following:

1. Start a transaction (so that either all, or nothing goes into the database)
2. Insert records into A, retrieving their PKs
3. Insert records into B, retrieving their PKs
4. Change the data in the dataset to reflect the new PKs from A and B as FKs in C
5. Insert records into C
6. Commit the transaction

If you were using SQL Server you could try a stored procedure (with relatively the same steps) but using Access does not allow you to do this.

Here is also the code to retrieve the ID with the insert (it's a VB example but I hope you will not have a problem with it):

http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B815629

And once again: belive it or not, this is how it is done in the practise.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 9

Expert Comment

by:_ys_
ID: 9632539
After calling DataAdapter::Update you'll need to call the DataSet's AcceptChanges method. This commits everything, and therfore not confuse another Update call.

When I say commit here. I'm not talking about a database commit - the DataSet still marks those tables and rows as per the DataRowState enumeration. AcceptChanges resets this status to unchanged.
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9632567
_ys_:
Extract from MSDN:

When using Update, the order of execution is as follows:

1. The values in the DataRow are moved to the parameter values.
2. The OnRowUpdating event is raised.
3. The command executes.
4. If the command is set to FirstReturnedRecord, then the first returned result is placed in the DataRow.
5. If there are output parameters, they are placed in the DataRow.
6. The OnRowUpdated event is raised.
7. AcceptChanges is called.

Because of step 7. you don't need to manually call AcceptChanges.
0
 
LVL 9

Expert Comment

by:_ys_
ID: 9632849
Never noticed that before. I've always called AcceptChanges.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now