Enable transaction operations to solve the time-consuming problem of batch inserting or updating databases such as sqlite and mssql.

Source: Internet
Author: User

Private void btnImport_Click (object sender, EventArgs e) {string filePath = textBox1.Text; string importPwd = txtPwd. text; if (string. isNullOrEmpty (filePath) | string. isNullOrEmpty (importPwd) {MessageBox. show ("Please import the file first, enter the operation password, and then operate again! ");} Else {btnImport. text = "importing... "; btnImport. enabled = false; string [] allLines = File. readAllLines (filePath); using (SQLiteConnection con = new SQLiteConnection (connStr) {con. open (); DbTransaction trans = con. beginTransaction (); // start the SQLiteCommand cmd = new SQLiteCommand (con); try {for (int n = 0; n <allLines. length; n ++) {cmd. commandText = "insert into imei (imei) values (@ imei)"; cmd. P Arameters. add (new SQLiteParameter ("@ imei", DbType. string); cmd. parameters ["@ imei"]. value = allLines [n]; cmd. executeNonQuery ();} trans. commit (); // submit the transaction MessageBox. show ("file imported successfully! ");} Catch (Exception ex) {trans. Rollback (); MessageBox. Show (" File Import error. Please check whether repeated import or other reasons! ") ;}Finally {btnImport. Text =" import "; btnImport. Enabled = true ;}}}}

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.