Batch update data--(there are also the original generations of the 51 Series)

Source: Internet
Author: User
My recent project has something to do with the database-share the analysis materials here-by the way, I am a computer repair engineer.

// -- The idea is to use a sqldataadapter to first retrieve data to the able, modify the data in the datatable to be updated, mainly by setting
Empadapter. updatebatchsize = 100; // -- the default value of this attribute indicates that the record is processed for the next time. Do not set it to too large.
Commandupdater. updatedrowsource = updaterowsource. None;
// ----------- Improve performance




Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Public partial class default3: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
// Data container
Datatable btmm = new datatable ();

Sqldataadapter empadapter = new sqldataadapter ();
// ----------- Select data
Sqlconnection dbconselect = new sqlconnection ();

// ------------- Copy data to this connection
Sqlconnection dbconupdater = new sqlconnection ();

Sqlcommand commandselect = new sqlcommand ();
Sqlcommand commandupdater = new sqlcommand ();

// Donet
Dbconselect. connectionstring = system. configuration. configurationmanager. connectionstrings ["connectionstring"]. connectionstring;
// Donet
Dbconupdater. connectionstring = system. configuration. configurationmanager. connectionstrings ["connectionstring"]. connectionstring;

// --------- Select the updated data
Commandselect. commandtext = "select * from B ";
Commandselect. commandtype = commandtype. text;
Commandselect. Connection = dbconselect;

// --------------------------------------- Set Update
Commandupdater. commandtext = "Update B set id = @ ID ";
Commandupdater. Connection = dbconupdater;

// -- Add Parameters
Sqlparameter A = new sqlparameter ("@ ID", sqldbtype. varchar, 50, "ID"); // note that the last address represents the Column Retrieved From the data source
Commandupdater. Parameters. Add ();
Empadapter. selectcommand = commandselect;
Empadapter. updatecommand = commandupdater;
Empadapter. Fill (btmm); // --- Call commandselect to retrieve data
Dbconselect. Close ();

Foreach (datarow DR in btmm. Rows)
{
Dr ["ID"] = "--ooo" + system. datetime. Now. tostring ();
}
// Update trigger
Empadapter. rowupdated + = new sqlrowupdatedeventhandler (onrowupdated );
Label1.text = "";
Empadapter. updatebatchsize = 100; // -- the default value of this attribute indicates that the record is processed for the next time. Do not set it to too large.
Commandupdater. updatedrowsource = updaterowsource. None;
//--------------------------------------------------------
// Both maps the output parameter and the first returned row to the modified row in the dataset.
// Firstreturnedrecord maps the data in the first returned row to the changed row in dataset.
// None ignores any returned parameters or rows.
// Outputparameters maps output parameters to changed rows in dataset.

Try
{
Dbconupdater. open ();
Empadapter. Update (blog );
}
Catch (exception)
{

}

}



Private void onrowupdated (Object sender, sqlrowupdatedeventargs ARGs)
{

Label1.text + = args. rowcount. tostring () + "<br> ";
}
}

// ------------ Web. confing settings
<Connectionstrings>
<Add name = "connectionstring" connectionstring = "Data Source =.; initial catalog = g; user id = sa; Password = sa"/>
<Add name = "connectionstringinfo" connectionstring = "Data Source =.; initial catalog = g; user id = sa; Password = sa"/>
</Connectionstrings>

 

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.