当前位置:Gxlcms > mysql > 启用事务操作,解决批量插入或更新sqlite,mssql等数据库耗时问_MySQL

启用事务操作,解决批量插入或更新sqlite,mssql等数据库耗时问_MySQL

时间:2021-07-01 10:21:17 帮助过:16人阅读

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;                    }                }            }        }

bitsCN.com

人气教程排行