C # insert tens of thousands of pieces of data into MySql in batches

Source: Internet
Author: User

In csdn ask, http://bbs.csdn.net/topics/390357952? Page = 1 # post-393577221, which adopted the comments of all heroes. At last, when I inserted 1-5 million pieces of data on the machine, it seemed that I didn't feel the time used.

I used MySQL's batch data format,

For example
Insert into tables (lable1, lable2, lable3 ,...)
Values (num11, num12, num13 ,...),
(Num21, num22, num23 ,...),
....
(Numn1, numn2, numn3 ,..);

As described earlier, each collection point of each account generates several hundred pieces of data. I combine these hundreds of data into the preceding statement, I have generated a total of to data items for each of the collection points under more than 20 accounts. Therefore, a total of to data items are inserted each time.

Each insert statement is saved in a sqlstringlist.
I found some information on the Internet and used the transaction processing method. Originally, my transaction method was to put all SQL statements in one transaction, but some enthusiastic friends told me that "commit every time 1000 dbcommands are executed
(COMMIT) transaction, and then start the transaction again, this is better. Put too many commands in a transaction. Once the physical memory allocation limit is exceeded, yourProgramIt will become very slow and slow ."

So I modified it and restarted the transaction every 500 statements. C #CodeAs follows:

[CSHARP] View plaincopy
    1. /// <Summary>
    2. /// Execute multiple SQL statements to implement database transactions.
    3. /// </Summary> MySQL database
    4. /// <Param name = "sqlstringlist"> Multiple SQL statements </param>
    5. Public Static VoidExecutesqltran (list <String> Sqlstringlist)
    6. {
    7. Using(Mysqlconnection conn =NewMysqlconnection (mysqlhelper. connstr ))
    8. {
    9. Conn. open ();
    10. Mysqlcommand cmd =NewMysqlcommand ();
    11. Cmd. Connection = conn;
    12. Mysqltransaction Tx = conn. begintransaction ();
    13. Cmd. Transaction = TX;
    14. Try
    15. {
    16. For(IntN = 0; n <sqlstringlist. Count; n ++)
    17. {
    18. StringStrsql = sqlstringlist [N]. tostring ();
    19. If(Strsql. Trim (). length> 1)
    20. {
    21. Cmd. commandtext = strsql;
    22. Cmd. executenonquery ();
    23. }
    24. // Added later
    25. If(N> 0 & (N % 500 = 0 | n = sqlstringlist. Count-1 ))
    26. {
    27. TX. Commit ();
    28. Tx = conn. begintransaction ();
    29. }
    30. }
    31. // Tx. Commit (); // The original one-time submission
    32. }
    33. Catch(System. Data. sqlclient. sqlexception E)
    34. {
    35. TX. rollback ();
    36. Throw NewException (E. Message );
    37. }
    38. }
    39. }
Related Article

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.