Answer the question
In order to leave comments, you need to log in
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
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)
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();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question