将 C# 数据集批量插入 AWS PostgreSql [英] Bulk insert C# dataset to AWS PostgreSql

查看:74
本文介绍了将 C# 数据集批量插入 AWS PostgreSql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想像 sqlbulkcopy 一样将 .net 中的数据表批量插入到 AWS postgres 表中.我尝试了复制命令,但它需要一些 pgadmin_reader 访问权限.请指导我如何解决这个问题.

I want to bulk insert data table in .net to AWS postgres table in single shot like sqlbulkcopy. I tried copy command but it require some pgadmin_reader acces. Please guide me how can i approach for this problem.

推荐答案

根据我的评论和您的后续问题,如果您从文件中复制 Npgsql,那么您需要超级用户权限.这是因为该文件将驻留在 PostgreSQL 服务器上,人们会认为很少有用户(希望只有管理员)可以访问甚至知道.

Following up on my comment and your subsequent question, if you do a Npgsql copy from a file, then you need superuser rights. This is because the file will reside on the PostgreSQL server, which one would think very few users (hopefully only admins) have access to or even know about.

但是,如果您从 STDIN 复制 - 标准输入,那么这本质上就是您将要提供的数据流.

However, if you copy from STDIN - standard input, then that is essentially a stream of data that you will then feed.

关于如何从 STDIN 复制的非常简短的入门/示例如下:

A very brief primer/example on how to copy from STDIN follows:

using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
{
    conn.Open();

    using (var writer = conn.BeginBinaryImport("copy my_table from STDIN (FORMAT BINARY)"))
    {
        foreach (object[] fields in myData)
        {
            writer.StartRow();
            writer.Write(fields[0]); // Assumes varchar
            writer.Write(fields[1], NpgsqlTypes.NpgsqlDbType.Integer);
            writer.Write(fields[2], NpgsqlTypes.NpgsqlDbType.Date);
        }

        writer.Complete();
    }
}

更多关于 Npgsql 帮助站点的信息.

Much more on the Npgsql help site.

这篇关于将 C# 数据集批量插入 AWS PostgreSql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆