开启事务,事务后台执行操作,并在执行完毕后提示

        string ConnString = @"Data Source=" + @"D:\SQLLITE\SQLiteStudio\集大成.db;Version=3";
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("即将后台更新对方科目,请勿在更新结束前关闭本软件");
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Start();//开始计时
            using (System.Data.SQLite.SQLiteConnection conntion = new System.Data.SQLite.SQLiteConnection(ConnString))
            {                
                conntion.Open();
                //将数据库数据加载至datable并添加年、月、日、凭账号的辅助列
                string Sql = "SELECT xsz.xz_gsmc as '公司名称',xsz.xz_nd as '年度',xsz.xz_y as '月',xsz.xz_r as '日',xsz.xz_pzh as '凭证号',group_concat(DISTINCT xsz.xz_kmmc) as '对方科目' FROM xsz GROUP BY xsz.xz_gsmc || xsz.xz_nd || xsz.xz_y || xsz.xz_r || xsz.xz_pzh";
                SQLiteDataAdapter da = new SQLiteDataAdapter(Sql, conntion);
                da.Fill(dt);
                //更新数据库表
				//新建线程
                Thread thread = new Thread(new ThreadStart(() =>
                {
                    using (System.Data.SQLite.SQLiteConnection conntion1 = new System.Data.SQLite.SQLiteConnection(ConnString))
                    {
                        conntion1.Open();
                        DataRow dr5 = null;
                        SQLiteTransaction ts = conntion1.BeginTransaction();
                        SQLiteCommand cmd = new SQLiteCommand();
                        cmd.Connection = conntion1;
                        cmd.Transaction = ts;
                        int a = dt.Rows.Count;
                        try
                        {
                            for (int i = 0; i < dt.Rows.Count; i++)//dt.Rows.Count; i++)
                            {
                                dr5 = dt.Rows[i];
                                cmd.CommandText = InsertToSql5(dr5);
                                cmd.Prepare();
                                //cmd.ExecuteNonQuery();
                                cmd.ExecuteNonQueryAsync();//异步执行                    
                            }
                            ts.Commit();//提交数据库事务
                            watch.Stop();//停止计时
                            MessageBox.Show("对方科目更新成功,共用时:" + (watch.ElapsedMilliseconds) / 1000 + "秒");
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }
            ));
                thread.Start();
            }            
            dataGridView1.DataSource = dt;
        }

为部分耗时较长的操作新建单独进程,实现后台运行,并在运行结束后提示是否成功运行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值