C# 从字符串数组创建sql查询
我的界面- 我正在尝试展开嵌套子查询-C# 从字符串数组创建sql查询,c#,oracle,oracle11g,C#,Oracle,Oracle11g,我的界面- 我正在尝试展开嵌套子查询- select * from jobs where (location='delhi' or location='Mumbai') and profile in(select profile from jobs where profile='CompScience'); 对于勾选的每个复选框,我想将其添加到条件中。 例如,如果勾选框为德里、孟买、康普科学 问题是- select * from jobs where (location='delhi' or
select * from jobs where (location='delhi' or location='Mumbai') and profile in(select profile from jobs where profile='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();
}
我尝试连接字符串,然后从中提取单个元素
编辑-
您可以有一个复选框数组,并使用
string.join()
:
如果在父容器(如groupbox或panel)上有复选框,您甚至可以执行以下操作:
CheckBox[] Locations = locPanel.Controls.OfType<CheckBox>().ToArray();
CheckBox[] Profiles = profPanel.Controls.OfType<CheckBox>().ToArray();
CheckBox[]Locations=locPanel.Controls.OfType().ToArray();
复选框[]Profiles=profPanel.Controls.OfType().ToArray();
然后,您可以通过以下方式创建查询命令(这只是针对位置的示例,对于配置文件您应该采用相同的方式):
根据Dmitry的回答,我建议将其进一步简化为
// ... do locations AND profiles the way Dmitry suggested
// start out with a generic query
StringBuilder querybuilder = new StringBuilder("SELECT * FROM jobs WHERE 1 = 1");
if (locations.Any())
{
var locationsString = string.Join(", ", locations.Select(l => $"'{l}'"));
querybuilder.AppendFormat(" AND location IN ({0})", locationsString);
}
if (profiles.Any())
{
var profilesString = string.Join(", ", profiles.Select(l => $"'{l}'"));
querybuilder.AppendFormat(" AND profile IN ({0})", profilesString);
}
// ...
OracleCommand comm2 = new OracleCommand(querybuilder.ToString(), conn);
catch all其中1=1
是创建动态组合查询的普遍接受的方法,它允许您极大地简化将变量子句附加到查询中的条件。为什么不在()
中执行一个位置?您当前的代码生成什么查询?明显的问题似乎是,您生成的查询缺少值周围的“
。可能是重复的Im获取空元组。我在使用的编辑中附加了代码…可能是我做错了什么?@ubuntu\u noob您的复选框数组是空的吗?我在GUI@ubuntu_noob我创造了同样的环境,同样的代码对我有用。当你点击按钮时,复选框被选中了吗?你能再解释一下代码吗?我没有完全理解它
CheckBox[] Locations = locPanel.Controls.OfType<CheckBox>().ToArray();
CheckBox[] Profiles = profPanel.Controls.OfType<CheckBox>().ToArray();
List<string> locations = new List<string>();
private void button1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
locations.Add(checkBox1.Text);
}
else
{
locations.Remove(checkBox1.Text);
}
// and so on for other locations
}
var locationsString = string.Join(", ", locations.Select(l => $"'{l}'")); // this gives you, e.x. 'Delhi', 'Mumbai'
var query = "";
if (locations.Any())
{
query = $"select * from jobs where(location in {locationsString }) and profile in(select profile from jobs where profile = 'CompScience');";
}
else
{
query = $"select * from jobs where profile in(select profile from jobs where profile = 'CompScience');";
}
// ... do locations AND profiles the way Dmitry suggested
// start out with a generic query
StringBuilder querybuilder = new StringBuilder("SELECT * FROM jobs WHERE 1 = 1");
if (locations.Any())
{
var locationsString = string.Join(", ", locations.Select(l => $"'{l}'"));
querybuilder.AppendFormat(" AND location IN ({0})", locationsString);
}
if (profiles.Any())
{
var profilesString = string.Join(", ", profiles.Select(l => $"'{l}'"));
querybuilder.AppendFormat(" AND profile IN ({0})", profilesString);
}
// ...
OracleCommand comm2 = new OracleCommand(querybuilder.ToString(), conn);