Steve C
Steve C

Reputation: 557

npgsql not returning rows when passed parameters in select statement

I am using the following code that was provided in the npgsql docs. I can retrieve data when my query string is Select * from public.accounts but this function will not return data when conditions are in the where clause.

I have seen other answers on SO that say to pass the command parameters in the AddWithValue function, but this doesn't return anything for me.

The query Select * from public.accounts where ext_auth0_user_id = 'github|42357689' DOES return data when I run it directly in pgAdmin, so I am assuming I have some formatting wrong.

        var userId = "github|42357689";
        using (var conn = new NpgsqlConnection(connString))
        {
            conn.Open();
            NpgsqlCommand cmd = new NpgsqlCommand("Select * from public.accounts where ext_auth0_user_id = @userId", conn);

            // Retrieve all rows
            using (cmd)
            {
                cmd.Parameters.AddWithValue("@userId", userId);
                using (NpgsqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        dataTable.Load(reader);
                    }
                }
            }
        }

Upvotes: 0

Views: 790

Answers (1)

Shapka
Shapka

Reputation: 21

NpgsqlParameter paramUserId = cmd.Parameters.Add("userId", NpgsqlTypes.NpgsqlDbType.Varchar, 20);

paramUserId.Direction = ParameterDirection.Input; paramUserId.value = userId;

NpgsqlParameter paramAnotherId = cmd.Parameters.Add("anotherId", NpgsqlTypes.NpgsqlDbType.Integer);

paramAnotherId.Direction = ParameterDirection.Input; paramAnotherId.value = anotherId;

Upvotes: 1

Related Questions