Solved

Refreshing DataSet w/ Dbase's Primary Keys

Posted on 2003-10-27
6
446 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 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

16 Experts available now in Live!

Get 1:1 Help Now