C#C
C#4y ago
12 replies
Jona4Play

✅ PostgreSQL foreach() insert not working

Hey, currently working on a DB project for the first time with PostgreSQL and for some reason this insert operation inserts the first entry in the list for the length of the list into the DB resulting in mass duplicate entries of just the first entry of the list.
var sql = @"INSERT INTO outstanding(name) VALUES(@name)";
            using var cmd = new NpgsqlCommand(sql, con);
            foreach (var item in outstanding)
            {
                cmd.Parameters.AddWithValue("name",item);
                cmd.Prepare();
                cmd.ExecuteNonQuery();
            }

What did I miss?
TYIA
Was this page helpful?