. Net batch insert data, dataset, sqldataadapter. Update

Source: Internet
Author: User

In. net, you can use the update method of the sqldataadapter class to update data to the database in batches, which improves the efficiency of writing data to the database,

Reduces the frequency of operating data and reduces the pressure on the database.

For more information about the method, see msdn Chinese http://technet.microsoft.com/zh-cn/library/system.data.sqlclient.sqldataadapter.aspx.

The update method has five loads. The following uses Update (datatable)
Let's take a simple example to write data into the database in batches.

The following is the implementation code list:

// The first step is to create a table structure and prepare the data inserted into the database. // If the table data comes from the query, you can use the dataset. Tables ["table name"]. Clone () method to perform a shortest copy. Datatable dt = new datatable ("user"); DT. columns. add ("userid", type. getType ("system. int32 "); DT. columns. add ("username", type. getType ("system. string "); DT. columns. add ("password", type. getType ("system. string "); DT. columns. add ("sex", type. getType ("system. boolean "); DT. columns. add ("Age", type. getType ("system. int32 "); int COUNT = 1000; // insert 1000 pieces of data into the database datarow DR = NULL; For (INT I = 0; I <count; I ++) {DR = DT. newrow (); Dr ["userid"] = I; Dr ["username"] = "Michael" + I; Dr ["password"] = "abc123456" + I; dr ["sex"] = I/2 = 0? True: false; Dr ["Age"] = 20 + I; DT. rows. add (DR); // use datatable. rows. add (datarow Dr): The data added to the able, datatable. the rowstate of each row of rows is added and sqldataadapter. the update method inserts row rowstate of the able data row into the database in batches. To learn more about rowstate, you can search for related information on the Internet, I will not go into details here.} // Database connection. You can customize using (sqlconnection conn = new sqlconnection ("Server = .; database = mytest; uid = sa; Pwd = justdoit ") {string SQL =" insert into [user] (userid, username, password, Sex, Age) values (@ userid, @ username, @ password, @ sex, @ age) "; sqlcommand cmd = new sqlcommand (SQL, Conn); cmd. parameters. add ("@ userid", sqldbtype. int, 8, "userid"); cmd. parameters. add ("@ username", sqldbtype. nvarchar, 20, "us Ername "); cmd. parameters. add ("@ password", sqldbtype. nvarchar, 50, "password"); cmd. parameters. add ("@ sex", sqldbtype. bit, 8, "sex"); cmd. parameters. add ("@ age", sqldbtype. int, 8, "Age"); sqldataadapter adapter = new sqldataadapter (); adapter. insertcommand = cmd; adapter. update (DT); // execute the insert data volume. // If Update (dataset, string) is used, insert the data to the dataset datatable before performing batch insert, // and adapter. update (datatable) is the same principle.}

The Code has been commented out in the code list,

Dataset: datatable is equivalent to the ing between databases and tables in memory. The data row in the datatable has a read-only attribute rowstate to identify the row status,

During the update operation, the data with rowstate as added is inserted into the database in batches. Using these features, you can easily and efficiently insert data into the database in batches.

Tested, completely OK.

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.