Home > other >  using prepared statements with Npgsql driver. Issue using IN
using prepared statements with Npgsql driver. Issue using IN

Time:08-28

    var param = puuids.Select(puuid => "'"   puuid   "'").ToList();
    const string query = "SELECT puuid,CAST(data AS text) as data FROM match WHERE puuid in (@puuid);";
    using var conn = _databaseFactory.GetDatabase();
    using var cmd = new NpgsqlCommand(query, conn);
    cmd.Parameters.Add(new NpgsqlParameter { ParameterName = "puuid", Value = string.Join(',', param) });
    cmd.Prepare();
    using var reader = cmd.ExecuteReader();

If I concatenate param into the query it works fine, but it's not ideal. I was wondering how i could get this to work with parameters.

CodePudding user response:

You can switch to =ANY, like puuid =ANY(?). It takes an array rather than a list, so then you can bind an entire array to one placeholder. I don't know how to bind an array to a placeholder in npgsql, but I assume there is a way.

  • Related