【发布时间】:2018-11-09 18:47:24
【问题描述】:
我的界面-
我正在尝试扩展我的嵌套子查询-
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();
}
【问题讨论】:
-
你为什么不简单地做一个
location IN ()?您当前的代码会产生什么查询?明显的问题似乎是您生成的查询缺少值周围的'。