Exemplo n.º 1
0
        private void btnDeleteNode_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("This action will delete the table from the metadata and data. Are you sure? ", "Dialog", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                PxMenuSelection selectedMenu = (PxMenuSelection)tvMenuSelection.SelectedNode.Tag;
                int             levelNo;
                int.TryParse(selectedMenu.LevelNo, out levelNo);
                if (levelNo < 5)
                {
                    //check if node has a table
                    if (tvMenuSelection.SelectedNode.Nodes.Count > 0)
                    {
                        MessageBox.Show("Could not delete this node it has subnodes, delete those first!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        string msg = "";
                        if (VariableFacade.Delete(selectedMenu, ref msg))
                        {
                            MessageBox.Show("The node was deleted", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
                //the node is a table
                else
                {
                    string      msg  = "";
                    string      msg2 = "";
                    string      msg3 = "";
                    PxMainTable mt   = VariableFacade.GetMainTableById(selectedMenu.Menu);

                    if (VariableFacade.Delete(mt, ref msg))
                    {
                        if (VariableFacade.Delete(selectedMenu, ref msg2))
                        {
                            if (mt != null)
                            {
                                if (VariableFacade.DeleteDataTable(mt.TableId, ref msg3))
                                {
                                    MessageBox.Show("The table was deleted", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                }
                                else
                                {
                                    MessageBox.Show(msg3, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }
                            }
                            else
                            {
                                if (VariableFacade.DeleteDataTable(selectedMenu.Menu, ref msg3))
                                {
                                    MessageBox.Show("The table was deleted", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                }
                                else
                                {
                                    MessageBox.Show(msg3, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }
                            }
                        }
                        else
                        {
                            MessageBox.Show(msg2, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }

                    else
                    {
                        MessageBox.Show(msg + "\n\n" + msg2 + "\n\n" + msg3, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static bool DataInsertInDb(PxMainTable mt, string excelPath, bool deleteExistingRowsFirst, ref string message)
        {
            if (System.IO.File.Exists(excelPath))
            {
                string qryExcel;
                //string qrySecondPart;
                //qrySecondPart = String.Format(" FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database={0};HDR=YES', 'SELECT * FROM [Data$]'", ExcelSource._path);
                qryExcel = "SELECT ";

                int countVariables;


                countVariables = mt.Variables.Count;


                qryExcel = GetExcelQueryForDataLoading(mt);

                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["PcAxisDatabase"].ConnectionString);
                try
                {
                    DataTable      dt         = GetSheetDataForInsert(qryExcel, excelPath);
                    List <DataRow> drToRemove = new List <DataRow>();

                    foreach (DataRow row in dt.Rows)
                    {
                        if (String.IsNullOrEmpty(row[0].ToString()))
                        {
                            drToRemove.Add(row);
                        }
                    }

                    foreach (DataRow row in drToRemove)
                    {
                        dt.Rows.Remove(row);
                    }
                    if (deleteExistingRowsFirst)
                    {
                        SqlCommand sc = new SqlCommand("DELTE FROM " + (mt.TableId + 1), conn);
                        conn.Open();
                        sc.ExecuteNonQuery();
                        conn.Close();
                    }
                    SqlBulkCopy sbc = new SqlBulkCopy(conn);
                    sbc.DestinationTableName = mt.TableId + 1;
                    conn.Open();
                    sbc.WriteToServer(dt);
                    conn.Close();
                    PxMetaModel.PcAxisMetabaseEntities context = new PxMetaModel.PcAxisMetabaseEntities();
                    mt.TableStatus = "O";
                    Save(mt, ref message);
                }
                catch (Exception ex)
                {
                    conn.Close();
                    message = ex.ToString();
                    return(false);
                }
                return(true);
            }
            return(false);
        }