About DataSet transaction processing and SqlDataAdapter usage, sqldataadapter usage

Source: Internet
Author: User

About DataSet transaction processing and SqlDataAdapter usage, sqldataadapter usage

If the SQL statement is executed directly, the transaction is well processed. For most Erp applications, SQL cannot be used to process data. Therefore, it is more common to update DataSet and update a single DataSet, no transaction processing is required. Adding transactions to multiple DataSet is mostly used in distributed program code. The following is the transaction processing code for updating the compressed DataSet passed by Winform in Webservice, multiple DataSet.

/// <Summary> /// update the compressed DataSet // </summary> /// <param> environment variable </param> /// <param> binary dataSet </param> /// <returns> has an exception, returns an error message </returns> public object UpdataSet (object [] env, byte [] byt) {object info = null; SqlConnection sqlconn = null; // define the transaction SqlTransaction transaction = null; DataSet ds = null; try {ds = md. bytesToDs (byt); string Url = "database connection language name"; sqlconn = new SqlConnection (Url); sqlconn. Open (); transaction = sqlconn. beginTransaction (IsolationLevel. readCommitted, "transname"); // instantiate the transaction SqlDataAdapter adapter; SqlCommandBuilder objCommandBuilder; for (int I = 0; I <ds. tables. count; I ++) {adapter = new SqlDataAdapter ("select * from" + ds. tables [I]. tableName + "where 2> 3", sqlconn); // traditional usage objCommandBuilder = new SqlCommandBuilder (adapter); // start to suspend/suspend the operation adapter. deleteCommand = new SqlCommand ("... delete statement .. ", sqlconn, transaction); // The addition, query, modification, and deletion of the adapter can be used with the transaction. Note that there is no instance SqlCommand object, so the new SqlCommand adapter. insertCommand = new SqlCommand ("............ ", sqlconn, transaction); adapter. updateCommand = new SqlCommand ("............. ", sqlconn, transaction); adapter. selectCommand = new SqlCommand ("select * from" + ds. tables [I]. tableName + "where 2> 3", sqlconn, transaction); // The new usage of adapter, After new SqlCommand, write SQL statements and SqlConnection. You can also write transactions... you do not need to use the instance SqlCommand object // you do not need to use the instance SqlCommand object to obtain the adapter. deleteCommand = objCommandBuilder. getDeleteCommand (); adapter. insertCommand = objCommandBuilder. getInsertCommand (); adapter. updateCommand = objCommandBuilder. getUpdateCommand (); objCommandBuilder. dataAdapter. update (ds, ds. tables [I]. tableName. toString (); //} transaction. commit (); // submit the transaction sqlconn. close ();} Catch (Exception err) {Console. out. writeLine ("An error occurred while saving the disk:" + err. message); info = "An error occurred while saving the disk:" + err. message; // transaction rollback transaction. rollback ();} finally {if (sqlconn! = Null) {sqlconn. Close () ;}} return info ;}

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.