In. Net, DataAdapter inserts and updates data in batches.

Source: Internet
Author: User

I have been busy launching projects some time ago. I encountered some problems that I had never encountered before while working on projects. Because I have no experience, I can only find some related solutions on the Internet, however, the information provided on the Internet is too complex, and some are useless at all, which is time-consuming and labor-consuming. I hope to record the problems I encountered during this period and help those who were as hungry as I was at that time to reduce the time cost, the solutions provided for these problems have been tested and are currently in use. Basically, there is no problem. If you have any questions, you can also discuss them. Because there are many problems, some of them cannot be remembered. I can only write something that can be remembered and shared with you. When searching for a question, you should try to use the keyword. It is not that you cannot find the answer to the question, but that you have a problem in searching for the question, and google is used as much as possible, when I used it, I knew why. What I couldn't understand was google translation. Batch insert in. when you operate on Big Data in. net, you may encounter a large amount of data insertion. In this way, we encounter the most stupid method: forearch insertion. A small amount of data is acceptable, but there are thousands of data records, tens of thousands of entries are dead. This method has poor performance. In this case, Microsoft cannot have a good solution. This is DataAdapter. I will paste the public static bool MultiInsertData (DataSet ds, string Columns, string tableName) inserted in batches) {using (OracleConnection connection = new OracleConnection (connectionString) {string SQLString = string. format ("select {0} from {1} where rownum = 0", Columns, tableName); using (OracleCommand cmd = new OracleCommand (SQLString, connection) {try {connection. open (); Oracle DataAdapter myDataAdapter = new OracleDataAdapter (); myDataAdapter. selectCommand = new OracleCommand (SQLString, connection); myDataAdapter. updateBatchSize = 0; OracleCommandBuilder custCB = new OracleCommandBuilder (myDataAdapter); DataTable dt = ds. tables [0]. copy (); DataTable dtTemp = dt. clone (); int times = 0; for (int count = 0; count <dt. rows. count; times ++) {for (int I = 0; I <400 & 400 * Times + I <dt. rows. count; I ++, count ++) {dtTemp. rows. add (dt. rows [count]. itemArray);} myDataAdapter. update (dtTemp); dtTemp. rows. clear ();} dt. dispose (); dtTemp. dispose (); myDataAdapter. dispose (); return true;} catch (System. data. oracleClient. oracleException E) {connection. close (); return false ;}}} all of you are engaged in programming. You can understand it at a Glance. in simple words, the ds parameter is the dataset to be updated, columns is the list to be inserted, and TableName is the name of the table to be inserted. It must be emphasized that the fields and classes of the table in the data set are inserted. Type must be consistent with that of the database. This is important; otherwise, an error will be reported during insertion. A multipart insertion is added here, that is, for, which is inserted once every 400 entries. This can avoid the insertion of too much data and timeout exception. This problem occurs when I create a project: Some data needs to be saved locally, such as SqlLite, and then uploaded to the database. It is recommended that you compress the dataset before transmission and decompress the dataset on the server. This will speed up the transmission. We have time to provide you with compression methods. The logic for batch update and batch insert is similar. I first paste the code: public static bool MultiUpdateData (DataTable data, string Columns, string tableName) {using (OracleConnection connection = new OracleConnection (connectionString) {string SQLString = string. format ("select {0} from {1} where rownum = 0", Columns, tableName); using (OracleCommand cmd = new OracleCommand (SQLString, connection) {try {connection. open (); OracleDataAdapter MyDataAdapter = new OracleDataAdapter (); myDataAdapter. selectCommand = new OracleCommand (SQLString, connection); OracleCommandBuilder custCB = new OracleCommandBuilder (myDataAdapter); custCB. conflictOption = ConflictOption. overwriteChanges; custCB. setAllValues = true; foreach (DataRow dr in data. rows) {if (dr. rowState = DataRowState. unchanged) dr. setModified ();} myDataAdapter. update (data); dat A. acceptChanges (); myDataAdapter. dispose (); return true;} catch (System. data. oracleClient. oracleException E) {connection. close (); return false ;}}}you can see that the code is similar to the one inserted above, but I have some points to note, the RowState of the dataset row must be Modified. If not, you need to set it. If not, the updated dataset must contain the primary key. If not, an error is returned. The important point is data. acceptChanges (); this code, if commented out, although myDataAdapter. update (data); the Code is updated, but the database still does not work. In my understanding, the Adapter itself means the Adapter, which is a bridge connecting two objects, like the Protocol, both sides can complete the entire process. Data. acceptChanges (); indicates the change of the application dataset. I tested that the application update can be updated to the database before the dataset is transferred. It may be dr. setModified (); this code is under control. If the data in the database changes in the dataset, the corresponding Uodate will be generated and updated. These are my guesses, I don't know much about the underlying things, which is also a weakness of mine. It must be done badly after the project is launched. The scenario of batch update is that the datagridview binds the queried data, modifies the data, and then updates the data to the database in batches.

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.