Exemplo n.º 1
0
        private void button_Click(object sender, RoutedEventArgs e)
        {
            if (textBox.Text == "")
            {
                System.Windows.Forms.MessageBox.Show("Нужно задать имя");
                return;
            }
            //SqlConnection con = new SqlConnection(Data.value);
            if (talk == 0)
            {
                type item = new type
                {
                    Название = textBox.Text
                };
                dbContex.type.InsertOnSubmit(item);
                try
                {
                    dbContex.SubmitChanges();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                /*string query = "INSERT INTO type (Название) VALUES(@Name)";
                 * con.Open();
                 * SqlCommand cmd = new SqlCommand(query, con);
                 * /*SqlCommand cmd2 = new SqlCommand("SELECT COUNT(*) FROM type", con);
                 * int count = 1;
                 * using (var reader = cmd2.ExecuteReader())
                 *  while (reader.Read())
                 *  {
                 *      count += (int)reader.GetValue(0);
                 *  }*/
                /*cmd.Parameters.AddWithValue("@ID", count);
                 * cmd.Parameters.AddWithValue("@Name", textBox.Text);
                 * cmd.ExecuteNonQuery();
                 * con.Close(); */
            }
            if (talk == 1)
            {
                Category categ = new Category
                {
                    Название = textBox.Text
                };
                dbContex.Category.InsertOnSubmit(categ);
                // Submit the change to the database.
                try
                {
                    dbContex.SubmitChanges();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                /*string query = "INSERT INTO Category (Название) VALUES(@Name)";
                 * con.Open();
                 * SqlCommand cmd = new SqlCommand(query, con);
                 * cmd.Parameters.AddWithValue("@Name", textBox.Text);
                 * cmd.ExecuteNonQuery();
                 * con.Close();*/
            }

            comboBox.Items.Add(textBox.Text);
            comboBox1.Items.Add(textBox.Text);
            textBox.Clear();
        }
Exemplo n.º 2
0
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (textBox1.Text != "")
            {
                if (textBox8.Text != "" && textBox9.Text != "")
                {
                    int Type_ID = 0, Categ_ID = 0;
                    int Address_ID;
                    try
                    {
                        /*SqlConnection con = new SqlConnection(Data.value);
                         * con.Open();*/

                        //Получаю id типа воды
                        //try
                        //{
                        var reader = from type in dbContex.type where type.Название == comboBox1.SelectedItem.ToString() select type.id_type;
                        foreach (var item in reader)
                        {
                            Type_ID = (int)item;
                            System.Windows.Forms.MessageBox.Show("ID Типа " + item.ToString());
                        }
                        if (getAdderss_ID(textBox8.Text, textBox9.Text) == -1)
                        {
                            Addres addr = new Addres
                            {
                                Широта  = textBox8.Text,
                                Долгота = textBox9.Text
                            };
                            dbContex.Addres.InsertOnSubmit(addr);
                            // Submit the change to the database.
                            try
                            {
                                dbContex.SubmitChanges();
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }

                        //Повторно отправляю запрос и получаю id Адреса
                        Address_ID = getAdderss_ID(textBox8.Text, textBox9.Text);

                        //Получаю id категории воды
                        var ID_Categs = from Category in dbContex.Category where Category.Название == comboBox1.SelectedItem.ToString() select Category.id_categ;
                        foreach (var item in ID_Categs)
                        {
                            Categ_ID = (int)item;
                            System.Windows.Forms.MessageBox.Show("ID Категории " + item.ToString());
                        }

                        /*SqlConnection con = new SqlConnection(Data.value);
                         * con.Open();
                         * string query = "INSERT INTO Prob_water (Название, Дата_забора, Консервация, Объем, id_Address, id_type, id_categ) VALUES(@Name, @Date, @Conserv, @V, @Address, @type, @categ)";
                         * SqlCommand cmd = new SqlCommand(query, con);
                         * cmd.Parameters.AddWithValue("@Name", textBox1.Text);
                         * cmd.Parameters.AddWithValue("@Date", Convert.ToDateTime(textBox2.Text));
                         * cmd.Parameters.AddWithValue("@Conserv", comboBox2.SelectedItem.ToString());
                         * cmd.Parameters.AddWithValue("@V", textBox.ToString());
                         * cmd.Parameters.AddWithValue("@Address", Address_ID);
                         * cmd.Parameters.AddWithValue("@type", Type_ID);
                         * cmd.Parameters.AddWithValue("@categ", Categ_ID);
                         * cmd.ExecuteNonQuery();
                         * con.Close();*/


                        //DateTime dt = DateTime.ParseExact(textBox2.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture);
                        //DateTime dot = Convert.ToDateTime(textBox2.Text).Date;
                        //string s = dt.ToString("dd.M.yyyy", CultureInfo.InvariantCulture);
                        //DateTime ndt = dt.Date;
                        //MessageBox.Show(ndt.ToString("d"));
                        Prob_water newProb = new Prob_water
                        {
                            Название    = textBox1.Text,
                            Дата_забора = Convert.ToDateTime(textBox2.Text),
                            Консервация = comboBox2.SelectedItem.ToString(),
                            Объем       = textBox.ToString(),
                            id_Address  = Address_ID,
                            id_type     = Type_ID,
                            id_categ    = Categ_ID
                        };
                        dbContex.Prob_water.InsertOnSubmit(newProb);
                        // Submit the change to the database.
                        try
                        {
                            dbContex.SubmitChanges();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        //string queryType = "SELECT * FROM type WHERE Название='" + comboBox1.SelectedItem.ToString() + "'";

                        /*SqlCommand comm = new SqlCommand(queryType, con);
                         * using (var read = comm.ExecuteReader())
                         * {
                         *  while (read.Read())
                         *  {
                         *      type = (int)read.GetValue(0);
                         *  }
                         * }*/
                        //}
                        //catch
                        //{
                        //System.Windows.Forms.MessageBox.Show("Введите тип воды");
                        //con.Close();
                        // return;
                        //}

                        //Получаю id Адреса


                        //Если запрос ничего не вернул заношу в таблицу координаты


                        /* string queryAddressId = "SELECT id_Address FROM Addres WHERE Широта = '" + textBox8.Text + "' AND Долгота = '" + textBox9.Text+"'";
                         * SqlCommand comm2 = new SqlCommand(queryAddressId, con);
                         * try
                         * {
                         *  using (var read = comm2.ExecuteReader())
                         *  {
                         *      while (read.Read())
                         *      {
                         *          idAddress += (int)read.GetValue(0);
                         *      }
                         *  }
                         * }
                         * catch
                         * {
                         *  System.Windows.Forms.MessageBox.Show("Не получил id запрос");
                         *  con.Close();
                         *  return;
                         * }
                         *
                         * //Если запрос ничего не вернул заношу в таблицу координаты
                         * if (idAddress == -1)
                         * {
                         *  string queryAddress = "INSERT INTO Addres (Широта, Долгота) VALUES (@latitude, @longitude)";
                         *  SqlCommand comm = new SqlCommand(queryAddress, con);
                         *  comm.Parameters.AddWithValue("@latitude", textBox8.Text);
                         *  comm.Parameters.AddWithValue("@longitude", textBox9.Text);
                         *  comm.ExecuteNonQuery();
                         * }
                         * //Повторно отправляю запрос и получаю id Адреса
                         * try
                         * {
                         *  using (var read = comm2.ExecuteReader())
                         *  {
                         *      while (read.Read())
                         *      {
                         *          idAddress = (int)read.GetValue(0);
                         *      }
                         *  }
                         * }
                         * catch
                         * {
                         *  System.Windows.Forms.MessageBox.Show("Не получил id адреса после занесения новых координат");
                         *  con.Close();
                         *  return;
                         * }
                         *
                         *
                         * //Получаю id категории воды
                         * try
                         * {
                         *  string queryCateg = "SELECT id_categ FROM Category WHERE Название='" + comboBox.SelectedItem.ToString() + "'";
                         *  SqlCommand comm = new SqlCommand(queryCateg, con);
                         *  using (var read = comm.ExecuteReader())
                         *  {
                         *      while (read.Read())
                         *      {
                         *          categ = (int)read.GetValue(0);
                         *      }
                         *  }
                         * }
                         * catch
                         * {
                         *  System.Windows.Forms.MessageBox.Show("Введите категорию воды");
                         *  con.Close();
                         *  return;
                         * }
                         * if (categ == 0)
                         * {
                         *  System.Windows.Forms.MessageBox.Show("Ничего не получилось категория не вернулась");
                         *  con.Close();
                         *  return;
                         * }
                         * if (type == 0)
                         * {
                         *  System.Windows.Forms.MessageBox.Show("Ничего не получилось тип не вернулся");
                         *  con.Close();
                         *  return;
                         * }
                         * string query = "INSERT INTO Prob_water (Название, Дата_забора, Консервация, Объем, id_Address, id_type, id_categ) VALUES(@Name, @Date, @Conserv, @V, @Address, @type, @categ)";
                         * SqlCommand cmd = new SqlCommand(query, con);
                         * cmd.Parameters.AddWithValue("@Name", textBox10.Text);
                         * cmd.Parameters.AddWithValue("@Date", textBox2.Text);
                         * cmd.Parameters.AddWithValue("@Conserv", comboBox2.SelectedItem.ToString());
                         * cmd.Parameters.AddWithValue("@V", textBox.ToString());
                         * cmd.Parameters.AddWithValue("@Address", idAddress);
                         * cmd.Parameters.AddWithValue("@type", type);
                         * cmd.Parameters.AddWithValue("@categ", categ);
                         * cmd.ExecuteNonQuery();
                         * con.Close();*/
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.ToString(), ex);
                    }
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("Не введены координаты");
                }
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Не введено имя пробы");
            }
        }