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

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

WBOY
WBOYOriginal
2016-06-01 13:17:561107browse

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn