我正在使用C# Npgsql,并试图使用COPY命令从Postgres导出数据。下面的查询将返回100+行。我想使用with循环来读取结果,就像使用NpgsqlDataReader一样。
当我试图获得第二行结果时,关于http://www.npgsql.org/doc/copy.html的文档总是抛出错误(第二行StartRow())。注意:运行第二个StartRow()将返回以下错误消息:
An unhandled exception of type 'Npgsql.NpgsqlException' occurred in mscorlib.dll
Additional information: Unknown message code: 0
using (NpgsqlBinaryExporter reader = conn.BeginBinaryExport("COPY tableName(colONE, colTWO) TO STDOUT (FORMAT BINARY)"))
{
reader.StartRow();
Console.WriteLine(reader.Read<string>());
Console.WriteLine(reader.Read<int>(NpgsqlDbType.Smallint));
reader.StartRow(); //### <== ERROR HERE ###
Console.WriteLine(reader.Read<string>());
Console.WriteLine(reader.Read<int>(NpgsqlDbType.Smallint));
}我如何读出所有的在一个WHILE循环中的行,或者一次输出?
发布于 2018-03-20 14:52:08
如果我正确地理解了这个问题,像这样的问题可能会有所帮助:
using (NpgsqlBinaryExporter reader = conn.BeginBinaryExport("COPY tableName(colONE, colTWO) TO STDOUT (FORMAT BINARY)"))
{
while(reader.StartRow() > 0)
{
Console.WriteLine(reader.Read<string>());
Console.WriteLine(reader.Read<int>(NpgsqlDbType.Smallint));
}
}它应该读取表中的所有数据,并解决了我类似的问题。我仍然在寻找提高性能的方法,所以如果你有什么想法的话,让我知道。
https://stackoverflow.com/questions/43246358
复制相似问题