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 1086193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:46:57+00:00 2026-05-16T22:46:57+00:00

I’m inserting large number of records using LinqToSql from C# to SqlServer 2008 express

  • 0

I’m inserting large number of records using LinqToSql from C# to SqlServer 2008 express DB. It looks like the insertion is very slow in this. Following is the code snippet.

public void InsertData(int id)
{

  MyDataContext dc = new MyDataContext();

  List<Item> result = GetItems(id);

  foreach (var item in result)
  {
    DbItem dbItem = new DbItem(){ItemNo = item.No, ItemName=item.Name};
    dc.Items.InsertOnSubmit();
  }

  dc.SubmitChanges();
}

Am I doing anything wrong? Or using Linq to insert large number of records is a bad choice?

Update: Thanks for all the answers.
@p.campbell: Sorry for the records count, it was a typo, actually it is around 100000. Records also range till 200k as well.

As per all the suggestions I moved this operation into parts (also a requirement change and design decision) and retrieving data in small chunks and inserting them into database as and when it comes. I’ve put this InsertData() method in thread operation and now using SmartThreadPool for creating a pool of 25 threads to do the same operation. In this scenario I’m inserting at a time only 100 records. Now, when I tried this with Linq or sql query it didn’t make any difference in terms of time taken.

As per my requirement this operation is scheduled to run every hour and fetches records for around 4k-6k users. So, now I’m pooling every user data (retrieving and inserting into DB) operation as one task and assigned to one thread. Now this entire process takes around 45 minutes for around 250k records.

Is there any better way to do this kind of task? Or can anyone suggest me how can I improve this process?

  • 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-05-16T22:46:57+00:00Added an answer on May 16, 2026 at 10:46 pm

    For inserting massive amount of data into SQL in a oner

    Linq or SqlCommand, neither are designed for bulk copying data into SQL.

    You can use the SqlBulkCopy class which provides managed access to the bcp utility for bulk loading data into Sql from pretty much any data source.

    The SqlBulkCopy class can be used to write data only to SQL Server tables. However, the data source is not limited to SQL Server; any data source can be used, as long as the data can be loaded to a DataTable instance or read with a IDataReader instance.

    Performance comparison

    SqlBulkCopy is by far the fastest, even when loading data from a simple CSV file.

    Linq will just generate a load of Insert statements in SQL and send them to your SQL Server. This is no different than you using Ad-hoc queries with SqlCommand. Performance of SqlCommand vs. Linq is virtually identical.

    The Proof

    (SQL Express 2008, .Net 4.0)

    SqlBulkCopy

    Using SqlBulkCopy to load 100000 rows from a CSV file (including loading the data)

    using (SqlConnection conn = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=EffectCatalogue;Data Source=.\\SQLEXPRESS;"))
    {
        conn.Open();
        Stopwatch watch = Stopwatch.StartNew();
    
        string csvConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\data\\;Extended Properties='text;'";
        OleDbDataAdapter oleda = new OleDbDataAdapter("SELECT * FROM [test.csv]", csvConnString);
        DataTable dt = new DataTable();
        oleda.Fill(dt);
    
        using (SqlBulkCopy copy = new SqlBulkCopy(conn))
        {
            copy.ColumnMappings.Add(0, 1);
            copy.ColumnMappings.Add(1, 2);
            copy.DestinationTableName = "dbo.Users";
            copy.WriteToServer(dt);
        }
        Console.WriteLine("SqlBulkCopy: {0}", watch.Elapsed);
    }
    

    SqlCommand

    using (SqlConnection conn = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TestDb;Data Source=.\\SQLEXPRESS;"))
    {
        conn.Open();
        Stopwatch watch = Stopwatch.StartNew();
        SqlCommand comm = new SqlCommand("INSERT INTO Users (UserName, [Password]) VALUES ('Simon', 'Password')", conn);
        for (int i = 0; i < 100000; i++)
        {
            comm.ExecuteNonQuery();
        }
        Console.WriteLine("SqlCommand: {0}", watch.Elapsed);
    }
    

    LinqToSql

    using (SqlConnection conn = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TestDb;Data Source=.\\SQLEXPRESS;"))
    {
        conn.Open();
        Stopwatch watch = Stopwatch.StartNew();
        EffectCatalogueDataContext db = new EffectCatalogueDataContext(conn);
        for (int i = 0; i < 100000; i++)
        {
            User u = new User();
            u.UserName = "Simon";
            u.Password = "Password";
            db.Users.InsertOnSubmit(u);
        }
        db.SubmitChanges();
        Console.WriteLine("Linq: {0}", watch.Elapsed);
    }
    

    Results

    SqlBulkCopy: 00:00:02.90704339
    SqlCommand: 00:00:50.4230604
    Linq: 00:00:48.7702995
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.