I have the following table in PostgreSQL:
+------------+----------+
| Column | Type |
+------------+----------+
| name | text |
| keywords | text[] |
+------------+----------+
I'd like to insert a whole lot of rows in it, but I can't figure out how to insert data into an array field. Using BeginTextImport does not work since the TextWriter doesn't accept arrays:
using (var writer = connection.BeginTextImport(
"COPY table (name, keywords) FROM STDIN DELIMITER ';'"
))
{
foreach (var item in items)
{
writer.Write(item.Name + ";");
// How to do this?
writer.Write(item.Keywords.ToArray(), NpgsqlDbType.Array | NpgsqlDbType.Text);
}
}
Using BeginBinaryImport gives an error: "Can't close writer, a row is still in progress, end it first NpgSql".
using (var writer = connection.BeginBinaryImport(
"COPY table (name, keywords) FROM STDIN (FORMAT BINARY)"
))
{
foreach (var item in items)
{
writer.StartRow();
writer.Write(item.Name, NpgsqlDbType.Text);
writer.Write(item.Keywords.ToArray(), NpgsqlDbType.Array | NpgsqlDbType.Text);
}
}// Exception thrown here
How can we bulk copy data into an array field?