title: Bulk data import into array field using Npgsql
body: <p>I have the following table in PostgreSQL:</p>
  
  <pre><code>+------------+----------+                   
  |   Column   |   Type   |
  +------------+----------+
  | name       | text     |
  | keywords   | text[]   |
  +------------+----------+
  </code></pre>
  
  <p>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 <code>BeginTextImport</code> does not work since the TextWriter doesn't accept arrays:</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>Using <code>BeginBinaryImport</code> gives an error: "<em>Can't close writer, a row is still in progress, end it first NpgSql</em>".</p>
  
  <pre><code>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
  </code></pre>
  
  <p>How can we bulk copy data into an array field?</p>