If I understand correctly NpgsqlCopyIn with NpgsqlCopySerializer should work something like this:
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();
}
Problem is that each time there are not allowed characters in AddString() it throws an ArgumentOutOfRangeException inside that method, otherwise it works.
For example:
npgsqlCopySerializer.AddString("another\r\naddress");
will throw the exception cause it contains a newline which has a special meaning in copy from text format (row separator) and should be escaped.
Anyone know what I can do to make it work? I searched for examples on the internet but I couldn’t find anything.
Thanks for your help!
I’m the person who reported the bug, coincidentally only a few days ago.
Would you mind posting the XML you’re trying to insert? I have tried it with an OSM result:
http://nominatim.openstreetmap.org/search.php?q=A7&viewbox=3.96%2C52.82%2C7.93%2C51.11&format=xml
And a rather large web.config file, both work flawlessly. So either I made a copy / paste error while submitting my fix, or the XML you’re trying to insert is different than the XML I tried.
Also: do you get the exact same error as before (i.e., argument out of range)? The default buffer size is only 8k, which wasn’t enough in my case.
I don’t know if it makes any difference, but I didn’t use flush at all: