Dil*_*lip 3 c# postgresql npgsql
我有一个包含 1000 条记录的数据表。我想执行从 C# 到 PGSQL 的批量插入操作。我知道一种将文本文件复制到的方法pgtable.
示例语法如下:
using (NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;User Id=postgres;Password=postgres;Database=postgres;"))
{
onn.Open();
NpgsqlCommand command = new NpgsqlCommand("copy \"schema\".\"tablename\" (\"col1\",\"col2\") from 'C:\\datafile.txt'", conn);
command.ExecuteNonQuery();
conn.Close();
}
Run Code Online (Sandbox Code Playgroud)
是否有任何其他函数可以用来传递数据表而不是将数据写入文本文件?我在用着Npgsql.dll。
您可能应该完整阅读COPY 的 PostgreSQL 文档。
COPY 可以用于导入 PostgreSQL 服务器文件系统中存在的文件(如代码示例所示),也可以用于从客户端复制数据,这可能就是您正在寻找的。后者是通过替换STDIN文件名来触发的。
如果您想使用 Npgsql 从客户端程序导入数据,请同时阅读Npgsql COPY 文档。对于文本数据导入,您可能需要调用NpgsqlConnection.BeginTextImport(),文档中有一个示例。