bitsCN.com
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; } } } }