Mass data Insertion database efficiency comparison test---ADO. NET under SqlBulkCopy () vs LINQ under Insertallonsubmit ()

Source: Internet
Author: User

Summary: Use. NET-related technology to insert large amounts of data into a database is a common operation. This article compares the two Technologies of ADO and LINQ, using the SqlBulkCopy () andinsertallonsubmit () methods respectively. It is concluded that the insertion efficiency of the same insertion effort (1w data) is 200 times times greater!

Test scenario:

Prepare two databases TestDB and TESTDB2, with table: T_users. The table structure looks like this:


The SqlBulkCopy () insertion method is as follows:

private static Stopwatch insertusingsqlbulkcopy (list<userinmemory> List) {Stopwatch Stopwatch = n            EW Stopwatch ();            Stopwatch.start ();            DataTable dt = new DataTable (); Dt.            Columns.Add ("ID"); Dt.            Columns.Add ("UserName"); for (int i = 0; i < list. Count; i++) {DataRow row = dt.                NewRow ();                row["ID"] = list[i].id; row["UserName"] = List[i].                UserName; Dt.            Rows.Add (row); } using (SqlConnection con = new SqlConnection (connStr2)) {con.                Open ();                        using (SqlBulkCopy bulkcopy = new SqlBulkCopy (con)) {try { Bulkcopy.destinationtablename = "dbo."                        T_username ";                        BULKCOPY.COLUMNMAPPINGS.ADD ("id", "id");                        BULKCOPY.COLUMNMAPPINGS.ADD ("UserName", "UserName"); BulkCopy.writetoserver (DT); } catch (Exception ex) {Console.WriteLine (ex.                    ToString ()); } finally {con. Close ();            }}} stopwatch.stop ();        return stopWatch; }

The LINQ insertion method is as follows:

private static Stopwatch Insertusinglinq (list<t_user> _list)        {            Stopwatch Stopwatch = new Stopwatch ();            Stopwatch.start ();            Dataclasses1datacontext dataContext = new Dataclasses1datacontext ();            DataContext.T_Users.InsertAllOnSubmit (_list);            Datacontext.submitchanges ();            Stopwatch.stop ();            return stopWatch;        }

Use the above code to import 10,000 user data into the database respectively.

The results are as follows:


The same insertion effort (1w data), the conclusion:

1 ADO. NET under the SqlBulkCopy () method is 266 times times greater than the Insertallonsubmit () method insertion Efficiency under LINQ to SQL!

2 LINQ is easy to use, short in code, low in learning, and elegant in language, but it is preferable to use the ADO method if it is highly efficient for large data-related operations.


Related Source: http://download.csdn.net/detail/fanrong1985/8130953

Mass data Insertion database efficiency comparison test---ADO. NET under SqlBulkCopy () vs LINQ under Insertallonsubmit ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.