Beispiel #1
0
        private string uploadTransaksiKartuMhs()
        {
            cSyncRun.isSync = true;    //tandai proses sync

            string err = string.Empty; //tampungan error message

            try
            {
                DataTable dt = null;
                cQuery    qr = new cQuery();
                cDatabase db = new cDatabase();

                dt = db.selectDataSqlite(qr.qSelectTransaksiMhsStatus0()); // ambil data dari sqlite
                if (dt.Rows.Count != 0)                                    //jika ada data
                {
                    cKoneksi koneksi = new cKoneksi();

                    SqlConnection    conn     = new SqlConnection(koneksi.konekMsSql('2'));   //konek ke sql server user ktm = 2
                    SQLiteConnection connLite = new SQLiteConnection(koneksi.LokasiSqlite()); //koneksi ke sqlite
                    try
                    {
                        conn.Open();     //open sql server
                        connLite.Open(); //open sqlite

                        //===inisialisasi progres bar======
                        if (PbUpload.InvokeRequired)
                        {
                            PbUpload.BeginInvoke(
                                new Action(() =>
                            {
                                PbUpload.Style   = ProgressBarStyle.Blocks;
                                PbUpload.Value   = 0;
                                PbUpload.Minimum = 0;
                                PbUpload.Maximum = dt.Rows.Count;
                            }
                                           ));
                        }
                        //=================================

                        SQLiteTransaction transactionSqlite = connLite.BeginTransaction();

                        DataTable dtTrMhs = null;
                        foreach (DataRow row in dt.Rows)
                        {
                            SqlCommand myCommand = null;

                            dtTrMhs = cekTransaksiMhs(row["nim"].ToString()); //cek data sebelumnya
                            if (dtTrMhs.Rows.Count == 0)                      //jika tidak ada data sebelumnya langung di insert
                            {
                                myCommand = new SqlCommand(qr.qInsertStatusMhs(), conn);

                                myCommand.Parameters.AddWithValue("@STUDENTID", row["nim"]);
                                myCommand.Parameters.AddWithValue("@TGL_AMBIL_KTM", row["lastModified"]);
                                myCommand.Parameters.AddWithValue("@PTGS_KTM_AMBIL", row["operator"]);
                            }
                            else //hanya di update
                            {
                                //==cek dulu status ktm diambil==
                                Int32  AMBIL_KTM_KE;
                                string tmpAMBIL_KTM_KE = "";
                                foreach (DataRow rowTrMhs in dtTrMhs.Rows)
                                {
                                    tmpAMBIL_KTM_KE = rowTrMhs["AMBIL_KTM_KE"].ToString().Trim();
                                }
                                //===============================

                                myCommand = new SqlCommand(qr.qUpdateStatusMhs(), conn);

                                if (Int32.TryParse(tmpAMBIL_KTM_KE, out AMBIL_KTM_KE))
                                {//jika sudah pernah diambil, urutan pengambilan ditambah 1
                                    myCommand.Parameters.AddWithValue("@AMBIL_KTM_KE", AMBIL_KTM_KE + 1);
                                }
                                else
                                {//jika urutan bukan int, set jadi 0
                                    myCommand.Parameters.AddWithValue("@AMBIL_KTM_KE", 1);
                                }
                                myCommand.Parameters.AddWithValue("@TGL_AMBIL_KTM", row["lastModified"]);
                                myCommand.Parameters.AddWithValue("@STUDENTID", row["nim"]);
                                myCommand.Parameters.AddWithValue("@PTGS_KTM_AMBIL", row["operator"]);
                            }

                            SQLiteCommand cmdSqlite = new SQLiteCommand(qr.qUpdateStatusUpload1(), connLite);
                            cmdSqlite.Parameters.AddWithValue("@idTransaksi", row["idTransaksi"]);

                            ////cek perintah thread close
                            if (cSyncRun.closingThread == true)
                            {
                                transactionSqlite.Commit(); //jika ada perintah thread close, maka di sqlite langsung di commit

                                conn.Close();               //tutup koneksi
                                conn.Dispose();
                                connLite.Close();
                                connLite.Dispose();

                                return("Thread Closed By User");
                            }

                            try
                            {
                                myCommand.ExecuteNonQuery();//insert ke sql
                            }
                            catch
                            {
                                transactionSqlite.Commit();//jika proses insert di sql gagal, di sqlite langsung di commit
                            }

                            cmdSqlite.ExecuteNonQuery();//update status ke sqlite

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

                            dtTrMhs.Clear();
                        }
                        transactionSqlite.Commit();
                    }
                    catch (Exception ex)
                    {
                        err = ex.Message;
                        //MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        conn.Close(); //tutup koneksi
                        conn.Dispose();
                        connLite.Close();
                        connLite.Dispose();
                    }
                }

                //clear datatable
                dt.Clear();
                dt.Dispose();
                //================
            }
            catch (Exception ex)
            {
                err = ex.Message;
            }

            cSyncRun.isSync = false;

            return(err);
        }