将多个值发送到 Mysql 查询
Send Multiple Values to Mysql Query
这是故事:
我有一个检查列表,其中包含我数据库中的项目需要编写查询以仅获取 selected 项目详细信息,然后填充 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;
}
}
}
我写了这段代码,它只得到了我 select 最后一项的详细信息。但我需要所有项目的详细信息 select
请不要担心在查询中不使用参数,因为这是一个示例
已更新。
也许您应该使用 StringBuilder
来完成这项任务。如果您对每个选定的项目都提出请求,效率也会非常低下。
像下面这样的东西可能是更好的解决方案:
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();
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;
}
}
尝试构建必须在 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 + ")";
您的问题是由循环内的项目值重置引起的。
你说不需要使用参数,但是在这种情况下使用它们并没有太复杂并且有更安全的代码
// 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;
}
这是故事:
我有一个检查列表,其中包含我数据库中的项目需要编写查询以仅获取 selected 项目详细信息,然后填充 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;
}
} }
我写了这段代码,它只得到了我 select 最后一项的详细信息。但我需要所有项目的详细信息 select
请不要担心在查询中不使用参数,因为这是一个示例
已更新。
也许您应该使用 StringBuilder
来完成这项任务。如果您对每个选定的项目都提出请求,效率也会非常低下。
像下面这样的东西可能是更好的解决方案:
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();
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;
}
}
尝试构建必须在 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 + ")";
您的问题是由循环内的项目值重置引起的。
你说不需要使用参数,但是在这种情况下使用它们并没有太复杂并且有更安全的代码
// 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;
}