W
W
White_Bambie2020-12-03 11:05:34
MySQL
White_Bambie, 2020-12-03 11:05:34

Display records from the database in a loop with an array filter?

It is necessary to unload the data from the database and transfer it to the report. But you need to filter the records through the array.
I tried output through a for loop, but for some reason it displays the last entry.

private void ReportTwoShow()
        {
            MySqlConnection con = new MySqlConnection(AppSetting.ConnectonString());

            string[] array = new string[] { "Запись 1", "Запись 2" };


            MySqlCommand cmd_FillTestimony; // Получаем данные из таблицы testimony
            cmd_FillTestimony = con.CreateCommand();
            cmd_FillTestimony.CommandText = "SELECT * FROM testimony";
// Я так понимаю предыдущая строка должна выглядеть как-то так: cmd_FillTestimony.CommandText = "SELECT * FROM testimony WHERE Name = '"+ array +"'";

            try
            {
                con.Open();

                MySqlDataAdapter da_testimony = new MySqlDataAdapter(cmd_FillTestimony);
                DataSet dataset_testimony = new DataSet();
                da_testimony.Fill(dataset_testimony, "testimony");

                report2.RegisterData(dataset_testimony.Tables["testimony"], "testimony");
                (report2.Report.FindObject("Testimony_Data") as DataBand).DataSource = report2.GetDataSource("testimony");
                report2.GetDataSource("testimony").Enabled = true;

                con.Close();

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

            report2.Show();
        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
Boris the Animal, 2020-12-03
@White_Bambie

You need to form one string like "@record1, @record2" ( parameterized queries ), and then pass the required values ​​as parameters.

SELECT * FROM testimony WHERE Name IN(@record1, @record2)

Something like this (I didn’t run the code, I quickly sketched it). Yes, the code can be written better, it's just an example to understand the idea. StringBuilder instead of string here will lead to a performance drop, it is faster to add small strings like this:
string sql = "SELECT * FROM testimony WHERE Name IN({0})";
string parameters = "";
string[] array = new string[] { "Запись 1", "Запись 2" };
for (int i = 0; i < array.Length; i++)
{
    parameters += "@record" + (i + 1) + (i < array.Length - 1 ? ", ": "");
}

using (SqlCommand cmd = new SqlCommand(string.Format(sql, parameters), this.connect))
{
    // Добавить параметры
    for (int i = 0; i < array.Length; i++)
    {
        cmd.Parameters.AddWithValue("@record" + (i + 1), array[i]);
    }

    cmd.ExecuteNonQuery();
}

B
BasiC2k, 2020-12-03
@BasiC2k

Here are holivars with solutions to your question: https://askdev.ru/q/parametrizaciya-predlozheniya-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question