Ejemplo n.º 1
0
        private string downloadOperatorTmp()
        {
            string err = string.Empty;

            try
            {
                cQuery    qr = new cQuery();
                cDatabase db = new cDatabase();
                DataTable dt = null;
                dt = db.selectData(qr.qSelectUserOpAkdk(), '1'); // ambil data dari mssql
                if (dt.Rows.Count != 0)                          //jika ada data
                {
                    //===inisialisasi progres bar======
                    if (ProgressBarSinkron.InvokeRequired)
                    {
                        ProgressBarSinkron.BeginInvoke(
                            new Action(() =>
                        {
                            ProgressBarSinkron.Style   = ProgressBarStyle.Blocks;
                            ProgressBarSinkron.Value   = 0;
                            ProgressBarSinkron.Minimum = 0;
                            ProgressBarSinkron.Maximum = dt.Rows.Count;
                        }
                                       ));
                    }

                    //=================================

                    cKoneksi koneksi = new cKoneksi();

                    using (var conn = new SQLiteConnection(koneksi.LokasiSqlite()))//koneksi ke sqlite
                    {
                        try
                        {
                            conn.Open();//buka koneksi
                            using (var cmd = new SQLiteCommand(conn))
                            {
                                using (var transaction = conn.BeginTransaction())
                                {
                                    //looping insert
                                    foreach (DataRow row in dt.Rows)
                                    {
                                        cmd.CommandText = qr.qInsertUserTmp();//" INSERT INTO userTmp (username, fullname, password, role) values(@username, @fullname, @password, @role)";
                                        cmd.Parameters.Clear();
                                        cmd.Prepare();
                                        cmd.Parameters.AddWithValue("@username", row["username"]);
                                        cmd.Parameters.AddWithValue("@fullname", row["fullname"]);
                                        cmd.Parameters.AddWithValue("@password", row["password"]);
                                        cmd.Parameters.AddWithValue("@aplication", row["aplication"]);
                                        cmd.Parameters.AddWithValue("@role", row["role"]);
                                        cmd.ExecuteNonQuery();

                                        //===proses progres bar======
                                        if (ProgressBarSinkron.InvokeRequired)
                                        {
                                            ProgressBarSinkron.BeginInvoke(
                                                new Action(() =>
                                            {
                                                ProgressBarSinkron.Increment(1);
                                            }
                                                           ));
                                        }

                                        //ProgressBarSinkron.Increment(1);
                                        //===========================
                                    }

                                    transaction.Commit();
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                            conn.Close(); //tutup koneksi
                            conn.Dispose();
                        }
                    }

                    ////===tutup progres bar======
                    //if (ProgressBarSinkron.Value == ProgressBarSinkron.Maximum)//jika sudah selesai tampilkan download complete
                    //{ LblProgressBar.Text = "Download Complete"; }
                    //MessageBox.Show("" + ProgressBarSinkron.Value + " Data Berhasil di Download");
                    //panelProgressBar.Visible = false;// hiden panel progressbar
                    ////==========================
                }

                //clear datatable
                dt.Clear();
                dt.Dispose();
                //================
            }
            catch (Exception ex)
            {
                err = ex.Message;
                //ProgressBarSinkron.Visible = false;
                MessageBox.Show(ex.Message);
            }
            return(err);
        }