Esempio n. 1
0
        // Скопировать запись
        private void bCopyLines_Click(object sender, EventArgs e)
        {
            // получаем названия датчиков из БД
            SensNameList = FormSensNameList(null, null);

            FormAddNewSensorsDB newForm = new FormAddNewSensorsDB();

            //newForm.ShowDialog();
            selType = lvwModels.FocusedItem.SubItems[0].Text.ToString();



            if (newForm.ShowDialog() == DialogResult.OK)
            {
                // Добавлем заданную модель в список моделей ListBox
                if ((newForm.newModelSens != "") && (newForm.newTypeSens != ""))
                {
                    // текст запроса
                    string query = "INSERT INTO Tsensors (Type, Model, NumOfRange) VALUES ('" + newForm.newTypeSens + "', '" + newForm.newModelSens + "', 2)";
                    // создаем объект OleDbCommand для выполнения запроса к БД MS Access
                    OleDbCommand command = new OleDbCommand(query, _сonnection);

                    try
                    {
                        // выполняем запрос к MS Access
                        command.ExecuteNonQuery();
                    }


                    catch
                    {
                        MessageBox.Show("Не удалось добавить заданную модель датчика БД. Возможно, такая модель уже есть в БД", "Добавление записи. Операция прервана", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    finally
                    {
                        //_сonnection.Close();
                    }


                    string strModel = newForm.newModelSens;
                    string strType  = newForm.newTypeSens;

                    string partQuery = "";

                    foreach (var gb in this.Controls.OfType <GroupBox>())
                    {
                        foreach (var tb in gb.Controls.OfType <NumericUpDown>())
                        {
                            if ((tb is NumericUpDown) && (tb.Tag != null))
                            {
                                partQuery = partQuery + tb.Name + "=" + "'" + tb.Text + "'" + ",";
                            }
                        }
                    }

                    foreach (var gb in this.Controls.OfType <GroupBox>())
                    {
                        foreach (var tb in gb.Controls.OfType <TextBox>())
                        {
                            if ((tb is TextBox) && (tb.Tag != null))
                            {
                                partQuery = partQuery + tb.Name + "=" + "'" + tb.Text + "'" + ",";
                            }
                        }
                    }


                    partQuery = partQuery.TrimEnd(new char[] { ',' });
                    // текст запроса

                    /*    string query = "UPDATE Tsensors SET " +
                     *                  "Serial = " + Serial.Text +
                     *                  ", Pmin = " + tbPmin.Text +
                     *                  ", NumOfRange = " + (Convert.ToInt16(rbRange2.Checked) + 1) +
                     *                  " WHERE Model = '" + str + "'";*/

                    query = "UPDATE Tsensors SET " +
                            partQuery +
                            ", NumOfRange = " + (Convert.ToInt16(rbRange2.Checked) + 1) +
                            ", VerModulePoint1='" + (VerModulePoint1.Text) + "'" +
                            ", VerModulePoint2='" + (VerModulePoint2.Text) + "'" +
                            ", VerModulePoint3='" + (VerModulePoint3.Text) + "'" +
                            " WHERE Type = '" + strType + "' AND Model = '" + strModel + "'";


                    // создаем объект OleDbCommand для выполнения запроса к БД MS Access
                    command = new OleDbCommand(query, _сonnection);

                    try
                    {
                        // выполняем запрос к MS Access
                        command.ExecuteNonQuery();
                        lvwModels.Items.Add(newForm.newTypeSens);
                        lvwModels.Items[lvwModels.Items.Count - 1].SubItems.Add(newForm.newModelSens);

                        lvwModels.Focus();
                        lvwModels.Select();
                        lvwModels.Items[lvwModels.Items.Count - 1].Focused  = true;
                        lvwModels.Items[lvwModels.Items.Count - 1].Selected = true;
                    }
                    catch
                    {
                        MessageBox.Show("Не удалось сохранить данные в файл с БД", "Сохранение данных. Операция прервана.", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    newForm.Close();
                    MessageBox.Show("Не заданы тип или модель датчика", "Добавление записи. Операция прервана", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }
Esempio n. 2
0
        //-----------------------------------------------------------------------------------------------

        // Обработчик добавить запись в БД
        private void bAddLines_Click(object sender, EventArgs e)
        {
            // получаем названия датчиков из БД
            SensNameList = FormSensNameList(null, null);

            FormAddNewSensorsDB newForm = new FormAddNewSensorsDB();

            //newForm.ShowDialog();

            if (newForm.ShowDialog() == DialogResult.OK)
            {
                // Добавлем заданную модель в список моделей ListBox
                if ((newForm.newModelSens != "") && (newForm.newTypeSens != ""))
                {
                    // текст запроса
                    string query = "INSERT INTO Tsensors (Type, Model, NumOfRange) VALUES ('" + newForm.newTypeSens + "', '" + newForm.newModelSens + "', 2)";
                    // создаем объект OleDbCommand для выполнения запроса к БД MS Access
                    OleDbCommand command = new OleDbCommand(query, _сonnection);

                    try
                    {
                        // выполняем запрос к MS Access
                        command.ExecuteNonQuery();
                        lvwModels.Items.Add(newForm.newTypeSens);
                        lvwModels.Items[lvwModels.Items.Count - 1].SubItems.Add(newForm.newModelSens);

                        lvwModels.Focus();
                        lvwModels.Select();
                        lvwModels.Items[lvwModels.Items.Count - 1].Focused  = true;
                        lvwModels.Items[lvwModels.Items.Count - 1].Selected = true;

                        foreach (var gb in this.Controls.OfType <GroupBox>())
                        {
                            foreach (var tb in gb.Controls.OfType <TextBox>())
                            {
                                if ((tb is TextBox) && (tb.Tag != null))
                                {
                                    tb.Text = "";
                                }
                            }
                        }
                        VerModulePoint1.SelectedIndex = -1;
                        VerModulePoint2.SelectedIndex = -1;
                        VerModulePoint3.SelectedIndex = -1;
                    }
                    catch
                    {
                        MessageBox.Show("Не удалось добавить заданную модель датчика БД. Возможно, такая модель уже есть в БД", "Добавление записи. Операция прервана", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    finally
                    {
                        //_сonnection.Close();
                    }
                }
                else
                {
                    newForm.Close();
                    MessageBox.Show("Не заданы тип или модель датчика", "Добавление записи. Операция прервана", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }