Запрос с пустым значением - MySQL

Узнай цену своей работы

Формулировка задачи:

Здраствуйте. Есть проблема. Нужно сделать в программе поиск по критериям автомобиля. Как сделать запрос чтобы если textBox пуст, отправлялся запрос с его пустым значенем, но чтобы искало все автомобили, а не те где поле пустое?

Решение задачи: «Запрос с пустым значением»

textual
Листинг программы
if (radioButton3.Checked == true && radioButton4.Checked == false)
            {
                cmd.CommandText = string.Format("SELECT type,mark,model,year,region,fuel,gearbox,mileage,price,lengine,id FROM avtomob WHERE buy=0 and ((type='{0}') or (type is null)) and ((mark='{1}') or (mark is null)) and ((model='{2}') or (model is null)) and ((year>='{3}') and (year<='{4}') or (year is null)) and ((region='{5}') or (region is null)) and ((fuel='{6}') or (fuel is null)) and ((gearbox='{7}') or (gearbox is null)) and ((mileage>='{8}') and (mileage<='{9}') or (mileage is null)) and ((price>='{10}') and (price<='{11}') or (price is null)) and ((lengine>='{12}') and (lengine<='{13}') or (lengine is null)) and ((hp>=0'{14}') and (hp<='{15}') or (hp is nuul)) and ((drive='{16}') or (drive is null))", comboBox13.Text, comboBox16.Text, textBox9.Text, comboBox14.Text, comboBox17.Text, comboBox15.Text, comboBox12.Text, comboBox5.Text, textBox14.Text, textBox13.Text, textBox12.Text, textBox11.Text, textBox10.Text, textBox15.Text, textBox8.Text, textBox16.Text, comboBox4.Text);
                MySqlDataReader reader;
                // читаем результат
                using (reader = cmd.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
 
                        while (reader.Read())
                        {
                            dataGridView4.Rows.Add();
                            dataGridView4[0, dataGridView4.Rows.Count - 1].Value = reader.GetString("type");
                            dataGridView4[1, dataGridView4.Rows.Count - 1].Value = reader.GetString("mark") + " " + reader.GetString("model") + " " + reader.GetString("year");
                            dataGridView4[2, dataGridView4.Rows.Count - 1].Value = reader.GetString("gearbox");
                            dataGridView4[3, dataGridView4.Rows.Count - 1].Value = reader.GetString("mileage") + " тис.км";
                            dataGridView4[4, dataGridView4.Rows.Count - 1].Value = reader.GetString("fuel") + ", " + reader.GetString("lengine");
                            dataGridView4[5, dataGridView4.Rows.Count - 1].Value = reader.GetString("region");
                            dataGridView4[6, dataGridView4.Rows.Count - 1].Value = reader.GetString("price") + " грн";
                            dataGridView4[7, dataGridView4.Rows.Count - 1].Value = reader.GetString("id");
                        }
                    }
                }
            }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

5   голосов , оценка 4.4 из 5
Похожие ответы