c# - 从字符串数组创建 sql 查询

标签 c# oracle oracle11g

我的界面-

enter image description here

我正在尝试扩展我的嵌套子查询-

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/

相关文章:

sql - 甲骨文 :Compare dates only by the Hour

sql - 如何在表上选择并计算某些值的出现次数

c# - 当记录应该是唯一的时重复记录

c# - 为什么我的 "Save"按钮不起作用?

c# - Windows窗体应用程序:发布问题

java - 如何通过 hibernate 将空字符串传递给日期类型的列

oracle - 创建一个存储过程,接受并计算该部门的总工资,并显示部门编号和总工资

c# - Nhibernate Group By 和 Alias To Bean

xml - 使用 PL/SQL 解析大型 XML 文件

sql - Oracle:将国家/地区分隔为 N 个边界