RE: Q: Datatables, Datasets and updating
- From: "Dale" <dale0973@xxxxxxxxxxxxx>
- Date: Sat, 28 Jan 2006 14:25:16 -0800
The problem seems to be that you have defined Primary Key to mean Identity
Column. Your question demonstrates one of the multitude of reasons why
identity columns are not generally suitable as primary keys. If your primary
keys were real attributes of your data, for instance SSN or "Orange" or
something like that, then you could always identify which item from table B
belongs to which item in table B.
You might like some of the heated debate on the subject on my blog at
http://www.dalepreston.com/Blog/2005/08/identity-crisis.html.
If you really must use an artificial key for your primary key, then you will
have to handle the database update yourself, inserting a row from table A,
returning SCOPE_IDENTITY after each insert, and then inserting the child rows
from table B substituting the temporary key you used in the DataSet with the
value of SCOPE_IDENTITY.
Then you can update the data in your DataSet with the SCOPE_IDENTITY value
to keep everything synchronized or just read the data back from the database
to update the DataSet.
HTH
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Geoff" wrote:
> Hi
>
> I'm hoping somebody can help me with the following problem that has occurred
> to me.
>
> Suppose I have two tables in an SQL Server database. Let's call these tables
> A and B. Assume that A has two fields: a primary key and another holding a
> string. In table B there are three fields: a primary key, a foreign key
> (which links to the primary key in A) and other field holding a string.
>
> Suppose I load these tables into a DataSet. I populate table A with some
> rows. For each of these rows I create some rows in B which are linked by the
> foreign key to A i.e. there is a one to many relationship between A and B.
> Once this is done, I update the DataSet via a DataAdaptor such that these
> tables are transferred to the SQL database.
>
> My question is this. As far as I can see, the values in the primary key of A
> and the corresponding keys in B will not necessarily be the same in the
> DataSet and the SQL database! Do you see what I mean? I can't see how the
> relationship specified is still valid after the Update.
>
> Can anybody explain this to me?
>
> Thanks in advance
>
> Geoff
>
>
>
>
.
- Follow-Ups:
- Re: Q: Datatables, Datasets and updating
- From: Geoff
- Re: Q: Datatables, Datasets and updating
- References:
- Q: Datatables, Datasets and updating
- From: Geoff
- Q: Datatables, Datasets and updating
- Prev by Date: Re: ? and : for conditional expressions in v2
- Next by Date: Extending Brian Connelly's C# CodeDOM Genetic Algorithm project
- Previous by thread: Q: Datatables, Datasets and updating
- Next by thread: Re: Q: Datatables, Datasets and updating
- Index(es):
Relevant Pages
|