. NET Bulk Update (INSERT, modify, delete) database

Source: Internet
Author: User
Tags bulk insert

Ideas: 1. Set the status identifier for each row in the DataTable, that is, the method to call the DataRow setadded (), SetModified (), Delete ()

2. Using the DataAdapter Update (DataTable) method

code example:

String connstring = "..."; Connection string

String SelectCommand = "SELECT * from item";

SqlDataAdapter da = new SqlDataAdapter (selectcommand,connstring);

SqlCommandBuilder cb = new SqlCommandBuilder (DA); Automatically generate the corresponding INSERT, UPDATE, DELETE statements

DataTable dt = new DataTable ();

Fill DT
Da. Fill (DT);

Update data and row status in DT (new, deleted, modified)
foreach (DataRow dr in Dt. Rows)
{
Code to update data (slightly)

Set line status

  Dr. Setadded ();
Dr. SetModified ();
Dr. Delete ();

}

Update to Database
da. Update (DT);

Note: If the amount of data involved is large, you should consider the database-side scenarios , such as: BULK INSERT into the database, and then do batch update processing on the database side

. NET Bulk Update (INSERT, modify, delete) database

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.