Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Primary Key column values after bulk insertion using SqlBulkCopy

Can anyone help me how to get the column values of a Primary Key column after bulk insertion?

I am using SqlBulkCopy to insert multiple rows at a time into table(say Table 1). I need the ids of a column MsgId(PK) from Table 1 once the rows get inserted. I need to use these ids which is the foreign key in another table Table 2

Here is my code,

        DataTable dtMessageDetails = new DataTable("Message Details");
        SqlBulkCopy copier = new SqlBulkCopy(Connectionstring);
        copier.DestinationTableName = "Table 1";
        copier.WriteToServer(dtMessageDetails);
like image 502
Sudha Avatar asked Dec 06 '25 03:12

Sudha


1 Answers

Lock the table before you insert your bulk. You know the number of rows and if you select the last id, knowing the total number of elements, then you will know their IDs.

like image 113
Lajos Arpad Avatar answered Dec 07 '25 16:12

Lajos Arpad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!