C # batch insert and update of massive data in China Sea

Source: Internet
Author: User

For the insertion and update of massive data, ADO. NET is indeed inferior to JDBC, and JDBC has a unified model for batch operations.
Very convenient:
PreparedStatement ps = conn. prepareStatement ("insert or update arg1, args2 ....");
Then you can
For (int I = 0; I <1000000000000000; I ++ ){
Ps. setXXX (realArg );
.....
Ps. addBatch ();
If (I % 500 = 0) {// assume that five hundred entries are submitted once.
Ps.exe cuteBatch ();
// Clear Parame Batch
}
}
Ps.exe cuteBatch ();
 
This operation not only brings about extremely high performance, but also is very convenient. To implement this function in ADO. NET, it should be directly in the Command interface.
Or the DataAdapter interface provides Addbat and CommitBat APIs, but ADO. NET does not implement this simply, but requires developers to pass
Complex workarounds.
For a large number of insert operations, you can use an empty able to Add rows to be inserted. after a certain number of rows are submitted, you can clear the table,
Implementation is not complicated:

 

DateTime begin = DateTime. Now;
String connectionString = ......;
Using (SqlConnection conn = new SqlConnection (connectionString ))...{
Conn. Open ();
SqlDataAdapter sd = new SqlDataAdapter ();
Sd. SelectCommand = new SqlCommand ("select devid, data_time, data_value from CurrentTest", conn );
Sd. InsertCommand = new SqlCommand ("insert into CurrentTest (devid, data_time, data_value )"
+ "Values (@ devid, @ data_time, @ data_value);", conn );
Sd. InsertCommand. Parameters. Add ("@ devid", SqlDbType. Char, 18, "devid ");
Sd. InsertCommand. Parameters. Add ("@ data_time", SqlDbType. Char, 19, "data_time ");
Sd. InsertCommand. Parameters. Add ("@ data_value", SqlDbType. Int, 8, "data_value ");
Sd. InsertCommand. UpdatedRowSource = UpdateRowSource. None;
Sd. UpdateBatchSize = 0;

DataSet dataset =

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.