Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8239349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:07:01+00:00 2026-06-07T20:07:01+00:00

Hi I am trying to enter large chunk of data from JSON to SQL

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:07:04+00:00Added an answer on June 7, 2026 at 8:07 pm

    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:

    CREATE TYPE item_drop_bulk_table_rev4 AS TABLE (
        item_id BIGINT,
        monster_class_id INT,
        zone_id INT,
        xpos REAL,
        ypos REAL,
        kill_time datetime
    )
    

    Second, create a procedure to insert the data:

    CREATE PROCEDURE insert_item_drops_rev4
        @mytable item_drop_bulk_table_rev4 READONLY
    AS
    
    BEGIN TRANSACTION
    
    -- Lookup the current ID
    DECLARE @id_marker bigint
    SELECT @id_marker = MAX(primary_key_column) FROM item_drops_rev4 
    
    -- Insert all the data
    INSERT INTO item_drops_rev4 
        (item_id, monster_class_id, zone_id, xpos, ypos, kill_time)
    SELECT 
        item_id, monster_class_id, zone_id, xpos, ypos, kill_time 
    FROM 
        @mytable
    
    -- Return back the identity values
    SELECT * FROM item_drops_rev4 WHERE primary_key_column > @id_marker
    
    COMMIT TRANSACTION
    

    Third, write the C# code to insert this data:

    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("item_id", typeof(Int64)));
    dt.Columns.Add(new DataColumn("monster_class_id", typeof(int)));
    dt.Columns.Add(new DataColumn("zone_id", typeof(int)));
    dt.Columns.Add(new DataColumn("xpos", typeof(float)));
    dt.Columns.Add(new DataColumn("ypos", typeof(float)));
    dt.Columns.Add(new DataColumn("timestamp", typeof(DateTime)));
    
    for (int i = 0; i < MY_INSERT_SIZE; i++) {
        dt.Rows.Add(new object[] { item_id, monster_class_id, zone_id, xpos, ypos, DateTime.Now });
    }
    
    // Now we&#039;re going to do all the work with one connection!
    using (SqlConnection conn = new SqlConnection(my_connection_string)) {
        conn.Open();
        using (SqlCommand cmd = new SqlCommand("insert_item_drops_rev4", conn)) {
            cmd.CommandType = CommandType.StoredProcedure;
    
            // Adding a "structured" parameter allows you to insert tons of data with low overhead
            SqlParameter param = new SqlParameter("@mytable", SqlDbType.Structured);
            param.Value = dt;
            cmd.Parameters.Add(param);
            SqlDataReader dr = cmd.ExecuteReader()
            // TODO: Read back in the objects, now attached to their primary keys
        }
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to accomplish the following: Have a thread that reads data from a
I'm trying to allow my users to enter in large lists of data using
I am trying to enter a foreign key value along with data collected from
I'm trying to enter a value in a specific records , I have a
I'm trying to require the user to enter data in at least one of
I'm trying to create a procedure that will enter data and then return a
I'm trying to make a loop so that a user can enter a number,
I have been trying to get my EditText box to word wrap, but can't
How can I do it? I was trying to enter some specified link (with
I have a large mysql table and I just noticed an issue when trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.