简体   繁体   中英

PostgreSQL COPY IN and NPGSQL

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

1 Tab one Return
2 Tab two Return
3 Tab three Return
4 Tab slony Return

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 and NpgsqlCopyIn declarations after connection.Open(); . Now works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM