示例#1
0
        // Удаление строки
        void RemoveRow()
        {
            var    page  = TabMain.SelectedTab;
            var    grid  = (DataGridView)page.Controls[0];
            string table = page.Name.Split('_')[1];

            /* Если выбелено более одной строки */
            if (grid.SelectedRows.Count > 1)
            {
                // Получаем индексы выделенных ячеек
                int[] indexes = new int[grid.SelectedRows.Count];

                for (int i = 0; i < grid.SelectedRows.Count; i++)
                {
                    indexes[i] = grid.SelectedRows[i].Cells[0].Value.exToInt();
                }

                if (new Remove(indexes, table, CheckMoreRelationships(table, indexes)).ShowDialog() == DialogResult.OK)
                {
                    Base.RemoveRows(table, indexes);
                    UpdateData(ROW_INDEX, true, true);                     // Обновление стобца
                }
            }
            /* Если выделена всего одна строка */
            else
            {
                if (new Remove(CheckRelationships(table, ID)).ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        switch (page.Name)
                        {
                        case "FPage_CarBrand": CarBrand.RemoveRow(ID); break;

                        case "FPage_CarModel": CarModel.RemoveRow(ID); break;

                        case "FPage_Cars": Cars.RemoveRow(ID); break;

                        case "FPage_CarType": CarType.RemoveRow(ID); break;

                        case "FPage_Drivers": Drivers.RemoveRow(ID); break;

                        case "FPage_Organizations": Organisations.RemoveRow(ID); break;

                        case "FPage_Staff": Staff.RemoveRow(ID); break;

                        case "FPage_StaffPosition": StaffPosition.RemoveRow(ID); break;
                        }
                        UpdateData(ROW_INDEX, true);                         // Обновление стобца
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message, "Ошибка при удалении", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
            }

            bool CheckRelationships(string initialTable, int id)
            {
                DataTable        checkingTable = new DataTable();
                OleDbDataAdapter adapter;

                // Проверка связанных таблиц
                using (var connection = new OleDbConnection(Base.DB_CONNECTION_STRING))
                {
                    /* +--------------------------------------------------+
                     * |											NOTE												|
                     * +--------------------------------------------------+
                     * |																									|
                     * |																									|
                     * | Имеют связь и требуют обработки									|
                     * +--------------------------------------------------+
                     * | CarType -> Cars ->								[TrackingLists]	|
                     * | CarBrand -> CarModel -> Cars ->	[TrackingLists]	|
                     * | StaffPosition -> Staff ->				[TrackingLists]	|
                     * |																									|
                     * | Не имеют особой связи, и не требуют обработки		|
                     * +--------------------------------------------------+
                     * | Estimate ->											[TrackingLists]	|
                     * | Drivers ->												[TrackingLists]	|
                     * | organisations ->									[TrackingLists]	|
                     * |																									|
                     * |																									|
                     * +--------------------------------------------------+
                     * |										END NOTE											|
                     * +--------------------------------------------------+
                     */
                    connection.Open();
                    // Выборка из БД, в зависимости от активной таблицы
                    switch (page.Name)
                    {
                    case "FPage_CarBrand":
                        adapter = new OleDbDataAdapter($"SELECT * FROM CarModel WHERE Carmodel_brand={id}", connection);
                        adapter.Fill(checkingTable);
                        break;

                    case "FPage_CarModel":
                        adapter = new OleDbDataAdapter($"SELECT * FROM Cars WHERE Car_model={id}", connection);
                        adapter.Fill(checkingTable);
                        break;

                    case "FPage_Cars":
                        adapter = new OleDbDataAdapter($"SELECT * FROM Trackinglists WHERE Trackinglist_transport={id}", connection);
                        adapter.Fill(checkingTable);
                        break;

                    case "FPage_CarType":
                        adapter = new OleDbDataAdapter($"SELECT * FROM Cars WHERE Car_type={id}", connection);
                        adapter.Fill(checkingTable);
                        break;

                    case "FPage_Drivers":
                        adapter = new OleDbDataAdapter($"SELECT * FROM Trackinglists WHERE Trackinglist_driver={id}", connection);
                        adapter.Fill(checkingTable);
                        break;

                    case "FPage_Organizations":
                        adapter = new OleDbDataAdapter($"SELECT * FROM Trackinglists WHERE " +
                                                       $"Trackinglist_organization={id} OR Trackinglist_organization_2={id}", connection);
                        adapter.Fill(checkingTable);
                        break;

                    case "FPage_Staff":
                        adapter = new OleDbDataAdapter($"SELECT * FROM Trackinglists WHERE " +
                                                       $"Trackinglist_dispatcher={id} OR Trackinglist_medic={id} OR Trackinglist_mechanic={id}", connection);
                        adapter.Fill(checkingTable);
                        break;

                    case "FPage_StaffPosition":
                        adapter = new OleDbDataAdapter($"SELECT * FROM Staff WHERE Staff_position={id}", connection);
                        adapter.Fill(checkingTable);
                        break;
                    }
                }

                if (checkingTable.Rows.Count != 0)
                {
                    return(true);                                               // Если есть строки то true
                }
                else
                {
                    return(false);                                                                                                      // Если нет строк то false
                }
            }

            bool CheckMoreRelationships(string initialTable, int[] values)
            {
                DataTable        checkingTable = new DataTable();
                OleDbDataAdapter adapter;

                #region Созданние строки запроса
                string CONDITION = "";
                string CONDITION_COLUMN = "", CONDITION_COLUMN2 = "", CONDITION_COLUMN3 = "";

                switch (page.Name)
                {
                case "FPage_CarBrand":
                    CONDITION_COLUMN = "Carmodel_brand";
                    break;

                case "FPage_CarModel":
                    CONDITION_COLUMN = "Car_model";
                    break;

                case "FPage_Cars":
                    CONDITION_COLUMN = "Trackinglist_transport";
                    break;

                case "FPage_CarType":
                    CONDITION_COLUMN = "Car_type";
                    break;

                case "FPage_Drivers":
                    CONDITION_COLUMN = "Trackinglist_driver";
                    break;

                case "FPage_Organizations":
                    CONDITION_COLUMN  = "Trackinglist_organization";
                    CONDITION_COLUMN2 = "Trackinglist_organization_2";
                    break;

                case "FPage_Staff":
                    CONDITION_COLUMN  = "Trackinglist_dispatcher";
                    CONDITION_COLUMN2 = "Trackinglist_medic";
                    CONDITION_COLUMN3 = "Trackinglist_mechanic";
                    break;

                case "FPage_StaffPosition":
                    CONDITION_COLUMN = "Staff_position";
                    break;
                }

                // Если в выборке только одно поле
                if (CONDITION_COLUMN2 == "" && CONDITION_COLUMN3 == "")
                {
                    for (int i = 0; i < values.Length; i++)
                    {
                        CONDITION += CONDITION_COLUMN + "=" + values[i] + " OR ";
                    }

                    CONDITION = CONDITION.Remove(CONDITION.Length - 4);
                }
                // Если в выборке 2 поля
                else if (CONDITION_COLUMN2 != "" && CONDITION_COLUMN3 == "")
                {
                    for (int i = 0; i < values.Length; i++)
                    {
                        CONDITION += CONDITION_COLUMN + "=" + values[i] + " OR ";
                    }

                    for (int i = 0; i < values.Length; i++)
                    {
                        CONDITION += CONDITION_COLUMN2 + "=" + values[i] + " OR ";
                    }

                    CONDITION = CONDITION.Remove(CONDITION.Length - 4);
                }
                // Если в выборке 3 поля
                else if (CONDITION_COLUMN2 != "" && CONDITION_COLUMN3 != "")
                {
                    for (int i = 0; i < values.Length; i++)
                    {
                        CONDITION += CONDITION_COLUMN + "=" + values[i] + " OR ";
                    }

                    for (int i = 0; i < values.Length; i++)
                    {
                        CONDITION += CONDITION_COLUMN2 + "=" + values[i] + " OR ";
                    }

                    for (int i = 0; i < values.Length; i++)
                    {
                        CONDITION += CONDITION_COLUMN3 + "=" + values[i] + " OR ";
                    }

                    CONDITION = CONDITION.Remove(CONDITION.Length - 4);
                }
                #endregion

                // Проверка связанных таблиц
                using (var connection = new OleDbConnection(Base.DB_CONNECTION_STRING))
                {
                    /* +--------------------------------------------------+
                     * |											NOTE												|
                     * +--------------------------------------------------+
                     * |																									|
                     * |																									|
                     * | Имеют связь и требуют обработки									|
                     * +--------------------------------------------------+
                     * | CarType -> Cars ->								[TrackingLists]	|
                     * | CarBrand -> CarModel -> Cars ->	[TrackingLists]	|
                     * | StaffPosition -> Staff ->				[TrackingLists]	|
                     * |																									|
                     * | Не имеют особой связи, и не требуют обработки		|
                     * +--------------------------------------------------+
                     * | Estimate ->											[TrackingLists]	|
                     * | Drivers ->												[TrackingLists]	|
                     * | organisations ->									[TrackingLists]	|
                     * |																									|
                     * |																									|
                     * +--------------------------------------------------+
                     * |										END NOTE											|
                     * +--------------------------------------------------+
                     */
                    connection.Open();
                    // Выборка из БД, в зависимости от активной таблицы
                    switch (page.Name)
                    {
                    case "FPage_CarBrand":
                        adapter = new OleDbDataAdapter($"SELECT * FROM CarModel WHERE {CONDITION}", connection);
                        adapter.Fill(checkingTable);
                        break;

                    case "FPage_CarModel":
                        adapter = new OleDbDataAdapter($"SELECT * FROM Cars WHERE {CONDITION}", connection);
                        adapter.Fill(checkingTable);
                        break;

                    case "FPage_Cars":
                        adapter = new OleDbDataAdapter($"SELECT * FROM Trackinglists WHERE {CONDITION}", connection);
                        adapter.Fill(checkingTable);
                        break;

                    case "FPage_CarType":
                        adapter = new OleDbDataAdapter($"SELECT * FROM Cars WHERE {CONDITION}", connection);
                        adapter.Fill(checkingTable);
                        break;

                    case "FPage_Drivers":
                        adapter = new OleDbDataAdapter($"SELECT * FROM Trackinglists WHERE {CONDITION}", connection);
                        adapter.Fill(checkingTable);
                        break;

                    case "FPage_Organizations":
                        adapter = new OleDbDataAdapter($"SELECT * FROM Trackinglists WHERE {CONDITION}", connection);
                        adapter.Fill(checkingTable);
                        break;

                    case "FPage_Staff":
                        adapter = new OleDbDataAdapter($"SELECT * FROM Trackinglists WHERE {CONDITION}", connection);
                        adapter.Fill(checkingTable);
                        break;

                    case "FPage_StaffPosition":
                        adapter = new OleDbDataAdapter($"SELECT * FROM Staff WHERE {CONDITION}", connection);
                        adapter.Fill(checkingTable);
                        break;
                    }
                }

                if (checkingTable.Rows.Count != 0)
                {
                    return(true);                                               // Если есть строки то true
                }
                else
                {
                    return(false);                                              // Если нет строк то false
                }
            }
        }