Мой интерфейс-
Я пытаюсь развернуть вложенный подзапрос-
select * from jobs where (location='delhi' or location='Mumbai') and profile in(select profile from jobs where profile='CompScience');
где для каждого из отмеченных флажков я хочу добавить это к условию.
Например, если отмечены флажки Дели, Мумбаи, CompScience
запрос будет -
select * from jobs where (location='delhi' or location='Mumbai') and profile in(select profile from jobs where profile='CompScience');
это моя попытка-
private void button1_Click(object sender, EventArgs e)
{
String location=null;
string profile-null;
if (checkBox1.Checked == true)
{
location+= checkBox1.Text;
}
if (checkBox2.Checked == true)
{
location += checkBox2.Text;
}
if (checkBox3.Checked == true)
{
location += checkBox3.Text;
}
if (checkBox4.Checked == true)
{
profile += checkBox4.Text;
}
if (checkBox5.Checked == true)
{
profile += checkBox5.Text;
}
if (checkBox6.Checked == true)
{
profile += checkBox6.Text;
}
//MessageBox.Show(location);
db_CONNECT();
conn.Open();
try
{
String query = "select * from jobs where(location= 'delhi' or location = 'Mumbai') and profile in(select profile from jobs where profile = 'CompScience');";
OracleCommand comm2 = new OracleCommand(selectquery, conn);
OracleDataAdapter MyAdapter = new OracleDataAdapter();//adapter acts as interface btw database and dataset(which is collectio of tables)
MyAdapter.SelectCommand = comm2;
DataTable dTable = new DataTable();//datatable represents a single table in database
MyAdapter.Fill(dTable);
dataGridView1.DataSource = dTable;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
conn.Close();
}
Я попытался объединить строки и затем извлечь из них отдельные элементы.
Edit-
private void button1_Click(object sender, EventArgs e)
{
db_CONNECT();
try
{
CheckBox[] Locations = { checkBox1, checkBox2, checkBox3 };
CheckBox[] Profiles = { checkBox4, checkBox5, checkBox6 };
string locs = string.Join(" or ", Locations.Where(c => c.Checked).Select(x => $"location = '{x.Text}'"));
string profs = string.Join(" or ", Profiles.Where(c => c.Checked).Select(x => $"profile = '{x.Text}'"));
string query = $"select * from jobs where ({locs}) and profile in(select profile from jobs where {profs})";
OracleCommand comm2 = new OracleCommand(query, conn);
OracleDataAdapter MyAdapter = new OracleDataAdapter();//adapter acts as interface btw database and dataset(which is collectio of tables)
MyAdapter.SelectCommand = comm2;
DataTable dTable = new DataTable();//datatable represents a single table in database
MyAdapter.Fill(dTable);
dataGridView1.DataSource = dTable;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
conn.Close();
}