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("請先導入文件,填寫操作密碼後,再操作!"); } else { btnImport.Text = "正在導入..."; btnImport.Enabled = false; string[] allLines = File.ReadAllLines(filePath); using (SQLiteConnection con = new SQLiteConnection(connStr)) { con.Open(); DbTransaction trans = con.BeginTransaction();//開始事務 SQLiteCommand cmd = new SQLiteCommand(con); try { for (int n = 0; n < allLines.Length; n++) { cmd.CommandText = "insert into imei(imei) values(@imei)"; cmd.Parameters.Add(new SQLiteParameter("@imei", DbType.String)); cmd.Parameters["@imei"].Value = allLines[n]; cmd.ExecuteNonQuery(); } trans.Commit();//提交事務 MessageBox.Show("文件導入成功!"); } catch (Exception ex) { trans.Rollback(); MessageBox.Show("文件導入錯誤,請檢查是否重復導入或其它原因!"); } finally { btnImport.Text = "導 入"; btnImport.Enabled = true; } } } }