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?
Solved just by moving
NpgsqlCommand
andNpgsqlCopyIn
declarations afterconnection.Open();
. Now works fine.