943,884 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 761
  • VB.NET RSS
Jan 5th, 2009
0

Datasets and SQLServer keys

Expand Post »
I'm new to datasets, and trying to update a SQL Server database with one. I'm having trouble understanding how to correctly write the dataset info to the server db and preserve the relationship keys.

When I write to the main table in the server database, it will assign the new entry a new ID for the primary key. But as I am writing to several child tables as well (that have a field that references the main ID), how do I make sure I record the correct ID for these tables? After all, I do not know the ID until the server gives it to me.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Tekito is offline Offline
56 posts
since Jan 2008
Jan 5th, 2009
0

Re: Datasets and SQLServer keys

Look when you treat with dataset you treat with database (I don't know if you've to set Enforce relationship property to true or it's set by default) so you can update it then reflect these changes to the physical database.
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Jan 6th, 2009
0

Re: Datasets and SQLServer keys

I'm not sure I understand the response, but I don't think I explained my problem very clearly, so I'll start over:

I am trying to add new entries to the SQL Server database from a dataset. The database has a main table with a primary key as the ID, then several child tables referenced to the main table via this ID.

When I start to add new entries to the database, the first thing I do is add a new entry to the main table. The database will then assign this entry a new ID. To finish the job I have to add some entries to the child tables, but I need to make sure to give these tables the new ID so everything is tied together properly. How do I read this new ID, or otherwise ensure integrity? Are you saying somehow the dataset and database figure this out automatically?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Tekito is offline Offline
56 posts
since Jan 2008
Jan 6th, 2009
0

Re: Datasets and SQLServer keys

My first answer is concept not for special case, once you load the database tables into your dataset, forget the physical database, and when you add entry to the primary table, you add it into dataset, so get the last entry key from the main table by anyway from dataset, then use it to add entries in child tables, and when you finish your work on the dataset, reflect that changes.
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Jan 6th, 2009
0

Re: Datasets and SQLServer keys

I think I understand.

One thing is that I was never actually loading the database into the dataset, only vice versa. This is because my program only adds to the database, and never needs to read anything from it. So I didn't bother loading from the database, I just created the dataset independently. I suppose if it is necessary to somehow tie the dataset and database together then I could do so.

The next question is what if someone else is also adding to the database at the same time? Are their changes going to be reflected to my dataset? Could this cause confusion in the process?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Tekito is offline Offline
56 posts
since Jan 2008
Jan 6th, 2009
0

Re: Datasets and SQLServer keys

As there are many users updating database, you need reflect dataset changes to the database once occurred. So you need to reflect\load and it's not good in performance perspective, so use Connected Mode, it's better in such cases.
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Jan 6th, 2009
0

Re: Datasets and SQLServer keys

Okay thanks for the advice... sounds like I need to learn a little bit more about datasets.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Tekito is offline Offline
56 posts
since Jan 2008
Jan 6th, 2009
0

Re: Datasets and SQLServer keys

Yes, preferable, and we're here to help you about anything you face, really I appreciate this word from you
Quote ...
sounds like I need to learn
, please mark it solved.
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: opening two external application inside my form
Next Thread in VB.NET Forum Timeline: How do I play a .wav file from Resources?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC