Exemplo n.º 1
0
        private void SaveDataToDB(string[] idList, Distibution distibution)
        {
            try
            {
                if (idList.Length > 0 && distibution != null)
                {
                    VcpmcContext vcpmcContext = new VcpmcContext();

                    //vcpmcContext.SaveChanges();
                    long count = 0;
                    for (int i = 0; i < idList.Length; i++)
                    {
                        foreach (var item in distributionDatas)
                        {
                            if (item.Id.ToString() == idList[i])
                            {
                                //3.save detail
                                foreach (var item2 in item.DistributionDataItems)
                                {
                                    vcpmcContext.DistributionDataItems.Add(item2);
                                    count++;
                                }
                                //2.save master2
                                //TODO
                                //item.DistibutionId = distibution.Id;
                                item.StatusSaveDataToDatabase = true;
                                vcpmcContext.DistributionDatas.Add(item);
                                break;
                            }
                        }
                        //break;
                    }
                    //
                    //1.save master
                    distibution.TotalRecord = count;
                    vcpmcContext.Distibutions.Add(distibution);

                    vcpmcContext.SaveChanges();
                    distibution = null;
                    dgvListFile.Invoke(new MethodInvoker(delegate
                    {
                        for (int i = 0; i < dgvListFile.RowCount; i++)
                        {
                            if ((bool)dgvListFile.Rows[i].Cells["StatusSaveDataToDatabase"].Value == true)
                            {
                                dgvListFile.Rows[i].Cells["StatusSaveDataToDatabase"].Style.BackColor = Color.Green;
                                dgvListFile.Rows[i].Cells["StatusSaveDataToDatabase"].Style.ForeColor = Color.White;
                            }
                            else
                            {
                                dgvListFile.Rows[i].Cells["StatusSaveDataToDatabase"].Style.BackColor = Color.Red;
                                dgvListFile.Rows[i].Cells["StatusSaveDataToDatabase"].Style.ForeColor = Color.White;
                            }
                        }
                    }));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
Exemplo n.º 2
0
        private void sbbtnSaveDataToDB_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Do you want to save data to database?", "Confirm Save data to Database",
                                    MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                                    MessageBoxDefaultButton.Button1) == System.Windows.Forms.DialogResult.Yes)
                {
                    #region Save
                    string listID = "";
                    dgvListFile.Invoke(new MethodInvoker(delegate
                    {
                        for (int i = 0; i < dgvListFile.Rows.Count; i++)
                        {
                            if (dgvListFile.Rows[i].Cells["Check"].Value != null)
                            {
                                if ((bool)dgvListFile.Rows[i].Cells["Check"].Value == true)
                                {
                                    string id = dgvListFile.Rows[i].Cells["Id"].Value.ToString();
                                    listID   += $"{id},";
                                }
                            }
                        }
                    }));

                    if (listID.Length > 0)
                    {
                        listID = listID.Substring(0, listID.Length - 1);
                        idList = listID.Split(',');
                        if (idList != null && idList.Length > 0)
                        {
                            distibution      = new Distibution();
                            distibution.Id   = Guid.NewGuid();
                            distibution.User = Core.User;
                            distibution.Name = $"(DIS-{distibution.User}-{ DateTime.Now.ToString("yyyy-MM-dd:HH-mm-ss")}";

                            operationDistribution = OperationDistribution.SaveToDB;
                            stStatus.Invoke(new MethodInvoker(delegate
                            {
                                stsStatus.Text      = "";
                                stsStatus.ForeColor = Color.White;
                                stsStatus.BackColor = Color.Green;
                                if (tstxtPath.Text.Trim() == "")
                                {
                                    stsStatus.Text      = "Saveing data to Database...";
                                    stsStatus.ForeColor = Color.White;
                                    stsStatus.BackColor = Color.Red;
                                    return;
                                }
                            }));
                            //cheAll
                            cheAll.Invoke(new MethodInvoker(delegate
                            {
                                cheAll.Checked = false;
                            }));

                            pcloader.Visible = true;
                            pcloader.Dock    = DockStyle.Fill;
                            backgroundWorker1.RunWorkerAsync();
                        }
                        else
                        {
                            MessageBox.Show("Please choise item that save data to database!");
                        }
                    }


                    #endregion
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Save data to database be error!");
            }
        }