我的界面-
我正在尝试扩展我的嵌套子查询-
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();
}
我尝试连接字符串,然后从中取出各个元素。
编辑-
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();
}
最佳答案
您可能有一个复选框数组,并使用 string.Join()
加入选中的文本:
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 result = $"select * from jobs where ({locs}) and profile in(select profile from jobs where {profs})";
如果您在父容器上有复选框,例如组框或面板,您甚至可以这样做:
CheckBox[] Locations = locPanel.Controls.OfType<CheckBox>().ToArray();
CheckBox[] Profiles = profPanel.Controls.OfType<CheckBox>().ToArray();
关于c# - 从字符串数组创建 sql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53223268/