0

这是故事:

我有一个检查列表,其中包含我的数据库中的项目需要编写查询以仅获取所选项目的详细信息,然后将其填充到 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;
                }

} }

我写了这段代码,它只让我了解我选择的最后一个项目的详细信息。但我需要我选择的所有项目的详细信息

请不要担心不在查询中使用参数,因为这是一个示例

4

4 回答 4

0

更新。也许你应该使用 aStringBuilder来完成这项任务。此外,如果您对每个选定的项目进行请求,效率也会非常低。类似以下内容可能是更好的解决方案:

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();
于 2016-09-13T11:19:16.257 回答
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;
    }
}
于 2016-09-13T11:27:26.657 回答
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 + ")";
于 2016-09-13T11:28:03.703 回答
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;
}
于 2016-09-13T11:39:17.947 回答