1

C# 新手,尝试将数组放入 IN() SQL 语句。换句话说,执行如下查询:

SELECT * FROM table WHERE something IN( [array_elem_1], [array_elem_2] ... )

在 PHP/MySQL 中相当容易做到:

mysql_query('SELECT * FROM table WHERE something IN("' . implode('","', array_map('mysql_real_escape_string', $values)) .'")');

但最好我能用 C#/ODBC 计算:

using (OdbcCommand cmd = new OdbcCommand("SELECT * FROM table WHERE something IN (" + String.Join(",", Enumerable.Repeat("?", values.Count)) + ")", conn))
{
    foreach (var kvp in values)
    {
        var param = cmd.CreateParameter();
        param.Value = kvp.Key;
        cmd.Parameters.Add(param);
    }
    cmd.ExecuteReader();
}

没有这么多代码,是否有更好的方法来做到这一点?谢谢!

4

0 回答 0