使用 npgsql 用数据填充 table 的最佳方法

The best way to fill table with data using npgsql

我正在使用 npgsql 连接到 PostgreSQL。此时,我通过手动生成数千条包含多个数据记录的 INSERT 语句来在数据库中填充 table

INSERT INTO tablename ( field1, field2 ) VALUES (1, "a"), (2, "b"), (3, "c") ...+ 1000 records;

并将它们放入 NpgsqlCommand.

如何以更有效的方式完成此操作?我听说过存储过程和 BULK 插入,但是非常需要一个很好的例子。

PostgreSQL 包含用于批量数据加载的 COPY 功能:http://www.postgresql.org/docs/9.4/static/sql-copy.html

Npgsql 在其 API 中公开 COPY。然而,Npgsql 的下一个主要版本 3.0 仍处于测试阶段,我们已经完全重做对它的 COPY 支持。因此,为 2.2 COPY API 编写代码并不是一个好主意,只是必须尽快从它迁移。如果您可以使用测试版,请使用 3.0,否则请等待几周直到它发布。

Npgsql 2.2 COPY API: https://github.com/npgsql/npgsql/wiki/User-Manual(搜索 "Fast bulk data copy into a table")

Npgsql 3.0 复制 API: http://npgsql.github.io/npgsql/doc/copy.html (temporary URL which will change to http://www.npgsql.org/doc/copy.html)