这是故事:
我有一个检查列表,其中包含来自我的数据库的项目需要编写查询以仅获取所选项目详细信息,然后填充gridviwe
object[] items = checkedListBox1.CheckedItems.OfType<object>().ToArray();
foreach (var item in items)
{
string connectionString = "server=127.0.0.1;uid=root;pwd=admin;database=per_update;";
string query = "select * from tblcenters where centerName in('" + item + "')";
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
using (MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn))
{
DataTable ds = new DataTable();
adapter.Fill(ds);
dataGridView1.DataSource = ds;
}
} }
我写了这段代码,它只能告诉我有关我选择的最后一项的详细信息。但我需要我选择的所有项目的详细信息
请不要担心在查询中不使用参数,因为这是一个示例
答案 0 :(得分:0)
<强>更新即可。
也许您应该使用StringBuilder sb = new StringBuilder();
sb.Append("select * from tblcenters where centerName in(");
for(int i=0;i<length;i++)
{
sb.Append('" + item[i] + "' + ",");
}
sb.Length--; // to get rid of the last comma (',')
sb.Append(")");
string query = sb.ToString();
来执行此任务。如果您对每个所选项目进行请求,效率也非常低。
以下内容可能是更好的解决方案:
{{1}}
答案 1 :(得分:0)
object[] items = checkedListBox1.CheckedItems.OfType<object>().ToArray();
string InStatement="("; //prepare the in clause
foreach (var item in items)
{
InStatement+=item+",";
}
InStatement=InStatement.Substring(0, InStatement.Length - 1)+")";///trim last comma and add ending parenthesis
string connectionString = "server=127.0.0.1;uid=root;pwd=admin;database=per_update;";
string query = "select * from tblcenters where centerName in"+InStatement;
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
using (MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn))
{
DataTable ds = new DataTable();
adapter.Fill(ds);
dataGridView1.DataSource = ds;
}
}
答案 2 :(得分:0)
尝试构建必须在WHERE IN
语句中使用的字符串,如下所示:
var whereValues = "";
foreach (var item in items) {
whereValues += ", '" + item + "'";
}
whereValues += whereValues.Substring(2, whereValues.Length-2);
string query = "select * from tblcenters where centerName in(" + whereValues + ")";
答案 3 :(得分:0)
您的问题是由重置循环内的项目值引起的 你说它不需要使用参数,但在这种情况下使用它们并没有太复杂,并且有一个更安全的代码
// Get all the checked items in a list of strings...
List<string> values = checkedListBox1.CheckedItems.Cast<string>().ToList();
List<MySqlParameter> prms = new List<MySqlParameter>();
List<string> prmsNames = new List<string>();
// Loop over the values and build the parameter names and the parameters
for (int x = 0; x < values.Count; x++)
{
// The parameter with a conventional name
prms.Add(new MySqlParameter("@" + x.ToString(), MySqlDbType.VarChar) { Value = values[x] });
// the parameter' names
prmsNames.Add("@" + x.ToString());
}
// The command text
string query = @"select * from tblcenters
where centerName in (" +
string.Join(",", prmsNames) + ")";
using (MySqlConnection conn = new MySqlConnection(connectionString))
using (MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn))
{
// Add the parameters collection to the select command....
adapter.SelectCommand.Parameters.AddRange(prms.ToArray());
DataTable ds = new DataTable();
adapter.Fill(ds);
dataGridView1.DataSource = ds;
}