C # insert tens of thousands of data

Source: Internet
Author: User

Insert tens of thousands of data records at a time

1.

[CSHARP]View plaincopy

  1. /// <Summary>
  2. /// Datatable batch add (with transactions)
  3. /// </Summary>/
  4. // <Param name = "table"> data source </param>
  5. /// <Param name = "mapping"> define a set of relationships between data sources and target source columns </param>
  6. /// <Param name = "destinationtablename"> target table </param>
  7. Public static bool mysqlbulkcopy (datatable table, sqlbulkcopycolumnmapping [] mapping, string destinationtablename ){
  8. Bool = true;
  9. Using (sqlconnection con = new sqlconnection (connectionstring )){
  10. Con. open ();
  11. Using (sqltransaction TRAN = con. begintransaction ()){
  12. Using (sqlbulkcopy copy = new sqlbulkcopy (con, sqlbulkcopyoptions. keepidentity, Tran )){
  13. Copy. destinationtablename = destinationtablename; // specify the target table
  14. If (mapping! = NULL) {// If data exists
  15. Foreach (sqlbulkcopycolumnmapping map in mapping ){
  16. Copy. columnmappings. Add (MAP );}}
  17. Try {copy. writetoserver (table); // batch add
  18. Tran. Commit (); // submit the transaction}
  19. Catch {Tran. rollback ();
  20. // Roll back the transaction bool = false ;}}}}
  21. Return bool ;}

2. MSSQL uses sqlbulkcopy to upload the data source, table name, and column shadow. It takes 1.5 million seconds to import the data.

[CSHARP]View plaincopy

  1. /// <Summary>
  2. /// Use sqlbulkcopy for batch insert. Only sqlserver
  3. /// Disadvantage: no number of returned rows
  4. /// </Summary>
  5. /// <Param name = "table"> the filled datatable supports other data sources. See reload. </param>
  6. /// <Param name = "tablename"> name of the table corresponding to the database </param>
  7. /// <Param name = "columns"> insert a column name set for the table. </param>
  8. Public void sqlbulkcopyinsert (datatable table, string tablename, string [] columns)
  9. {
  10. Sqlbulkcopy SBC = new sqlbulkcopy ("successive strings ");
  11. SBC. destinationtablename = tablename;
  12. Foreach (string Col in columns)
  13. {
  14. SBC. columnmappings. Add (COL, col );
  15. }
  16. SBC. writetoserver (table );
  17. }

3. For other databases, find a datatable, fill the data in the table, and then in adpt. Update (table)

[CSHARP]View plaincopy

    1. /// <Summary>
    2. /// Insert multiple rows. Check the connection/command/dataadapter for the database type you are connected.
    3. /// Replace the value
    4. /// </Summary>
    5. /// <Param name = "ds"> dataset after data filling </param>
    6. /// <Returns> Number of affected rows </returns>
    7. Public int multyinsert (Dataset DS)
    8. {
    9. Int result = 0;
    10. Idbconnection con = new oracleconnection ("connection string ");
    11. Con. open ();
    12. Idbcommand cmd = new oraclecommand ();
    13. Cmd. commandtext = "insert into member (username, password) values (@ name, @ password )";
    14. Idbdataparameter namepar = cmd. createparameter ();
    15. Namepar. parametername = "@ name ";
    16. Namepar. sourcecolumn = "username ";
    17. Namepar. sourceversion = datarowversion. Original;
    18. Namepar. dbtype = dbtype. String;
    19. Cmd. Parameters. Add (namepar );
    20. Idbdataparameter passpar = cmd. createparameter ();
    21. Passpar. parametername = "@ pass ";
    22. Passpar. dbtype = dbtype. String;
    23. Passpar. sourcecolumn = "password ";
    24. Passpar. sourceversion = datarowversion. Original;
    25. Cmd. Parameters. Add (passpar );
    26. Idbdataadapter adpt = new oracledataadapter ();
    27. Adpt. insertcommand = cmd;
    28. Try
    29. {
    30. Result = adpt. Update (DS );
    31. }
    32. Catch (exception)
    33. {
    34. Throw;
    35. }
    36. Finally
    37. {
    38. Con. Close ();
    39. }
    40. Return result;
    41. }

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.