المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Retrieve the PK after an add into an in memory DataTable



C# Programming
07-07-2009, 12:51 AM
I know how to do this in SQL but I must do it from C#. The DataTable is actually in memory (an xsd).

So I have some DataSet called "Converted" and in it is a Table called "Results" which has a relationship with another table called "Descriptions".

I am converting some data (thus the name) to an in memory table I have created. The problem is that I need the Primary Key of the "Results" last insert in order to populate the "Descriptions". How can I get the last inserted primary key?


The bellow code shows what I am doing. In the section Descriptions is where various paramaters from the raw data are then combined into multiple entries into the Descriptions table. I ofcourse need to link them back to the current row I am processing.


foreach (RawData.ResultsRow rRow in _rawData.Results)
{
Converted.ResultsRow nRow = MemDataSet.Results.NewResultsRow();
nRow.Param = rRow.SomeParam
MemDataSet.Results.AddResultsRow(nRow);

//int pk = ??????
...
#region Descriptions
ConvertedDescriptionsRow dRow = MemDataSet.Descriptions.NewDescriptionsRow();
dRow.Param = rRow.SomeDescriptor
dRow.ResultsFK = pk;
MemDataSet.Descriptions.AddDescriptionsRow(dRow);
#endregion Descriptions
}


"9 Pregnent woman can not have a baby in 1 month" -Uknown