Esempio n. 1
0
        private string downloadKendaraanTmp()
        {
            string err = string.Empty;

            try
            {
                cQuery    qr = new cQuery();
                cDatabase db = new cDatabase();
                DataTable dt = null;
                dt = db.selectData(qr.qSelectKendaraan(), '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.qInsertKendaraanTmp();
                                        cmd.Parameters.Clear();
                                        cmd.Prepare();
                                        cmd.Parameters.AddWithValue("@id", row["id_kendaraan"]);
                                        cmd.Parameters.AddWithValue("@id_pengguna", row["pengguna"]);
                                        cmd.Parameters.AddWithValue("@no_kendaraan", row["tnkb"]);
                                        cmd.Parameters.AddWithValue("@pemilik", row["pemilik"]);
                                        cmd.Parameters.AddWithValue("@merek", row["merek"]);
                                        cmd.Parameters.AddWithValue("@jenis", row["id_jenis_kendaraan"]); //jenis dilangsung aja
                                        cmd.Parameters.AddWithValue("@last_update", row["last_update"]);  //last Update
                                        cmd.ExecuteNonQuery();

                                        //===proses progres bar======
                                        if (ProgressBarSinkron.InvokeRequired)
                                        {
                                            ProgressBarSinkron.BeginInvoke(
                                                new Action(() =>
                                            {
                                                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);
        }