Hi I am trying to enter large chunk of data from JSON to SQL Server, So according to what I have researched I Can Convert the JSON to DataTable and then use SQLBulk to Write the Data to Server using WriteToSever(dataTable). Is that the Best way to do it ?
And How Will I be able to extract Scope_identity() for each row Insert from this way?
Extracting scope identity isn’t really possible from a bulk insert command. If you want to accurately get back every single scope_identity() value in an insert statement, you really have to insert each record individually.
However, you could also consider using a table parameter insert (I wrote an article about it here: http://www.altdevblogaday.com/2012/05/16/sql-server-high-performance-inserts/).
First, create a table insert type:
Second, create a procedure to insert the data:
Third, write the C# code to insert this data:
Now, in theory, you could retrieve back only the single “id marker”. However, I’m not convinced that every single version of SQL Server out there will always insert records from a datatable insert in linear order, so I think it’s safer to retrieve back the whole data batch.