Solved

Refreshing DataSet w/ Dbase's Primary Keys

Posted on 2003-10-27
6
452 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I need help adding validation to my MVC.Net view 6 35
Trigger C# code inside the SQL Server 6 57
C# DateTime Help 6 46
asp.net repeater server posting 5 37
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 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