PostgreSQL COPY IN and NPGSQL

2.1k views Asked by At

I'm trying to copy data from stream to postgres.
I've prepared file fl.ext:

1TaboneReturn
2TabtwoReturn
3TabthreeReturn
4TabslonyReturn

And wrote test code:

StreamReader sr = new StreamReader(Console.ReadLine());
NpgsqlCommand cmd = new NpgsqlCommand(string.Format("COPY {0}(id, text) FROM STDIN;", tableName), connection);
NpgsqlCopyIn cin = new NpgsqlCopyIn(cmd, connection, sr.BaseStream);
connection.Open();
cin.Start();
cin.End();
cin.Close();

But it fails on cin.Start() with {"Object reference not set to an instance of an object."} and no additional details.
What I'm doind wrong, guys?

1

There are 1 answers

0
Arman Hayots On

Solved just by moving NpgsqlCommand and NpgsqlCopyIn declarations after connection.Open();. Now works fine.