Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use NpgsqlCopyIn with NpgsqlCopySerializer?
    text
    copied!<p>If I understand correctly NpgsqlCopyIn with NpgsqlCopySerializer should work something like this:</p> <pre><code>var conn = new NpgsqlConnection(ConfigurationManager.ConnectionStrings["PostgreSqlDb"].ConnectionString); conn.Open(); var tran = conn.BeginTransaction(); var cmd = new NpgsqlCommand("COPY address (id, employee, value) FROM STDIN", conn); var npgsqlCopySerializer = new NpgsqlCopySerializer(conn); var npgsqlCopyIn = new NpgsqlCopyIn(cmd, conn, npgsqlCopySerializer.ToStream); try { npgsqlCopyIn.Start(); npgsqlCopySerializer.AddInt32(300); npgsqlCopySerializer.AddInt32(1); npgsqlCopySerializer.AddString("address"); npgsqlCopySerializer.EndRow(); npgsqlCopySerializer.Flush(); npgsqlCopySerializer.AddInt32(301); npgsqlCopySerializer.AddInt32(1); npgsqlCopySerializer.AddString("another\r\naddress"); npgsqlCopySerializer.EndRow(); npgsqlCopySerializer.Flush(); npgsqlCopyIn.End(); tran.Commit(); } //catch (Exception e) //{ // tran.Rollback(); // throw; //} finally { conn.Close(); } </code></pre> <p>Problem is that each time there are not allowed characters in <code>AddString()</code> it throws an <code>ArgumentOutOfRangeException</code> inside that method, otherwise it works.</p> <p>For example:</p> <pre><code>npgsqlCopySerializer.AddString("another\r\naddress"); </code></pre> <p>will throw the exception cause it contains a newline which has a special meaning in <a href="http://www.postgresql.org/docs/8.3/interactive/sql-copy.html" rel="nofollow">copy from text format</a> (row separator) and should be escaped.</p> <p>Anyone know what I can do to make it work? I searched for examples on the internet but I couldn't find anything.</p> <p>Thanks for your help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload