如何将NpgsqlCopyIn与NpgsqlCopySerializer一起使用? [英] How to use NpgsqlCopyIn with NpgsqlCopySerializer?

查看:244
本文介绍了如何将NpgsqlCopyIn与NpgsqlCopySerializer一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我对NpgsqlCopySerializer的理解正确,那么NpgsqlCopyIn应该像这样工作:

If I understand correctly NpgsqlCopyIn with NpgsqlCopySerializer should work something like this:

var conn = new NpgsqlConnection(ConfigurationManager.ConnectionStrings["PostgreSqlDb"].ConnectionString);
conn.Open();
var tran = conn.BeginTransaction();
var cmd = new NpgsqlCommand("COPY address (id, employee, value) FROM STDIN", conn);
var npgsqlCopySerializer = new NpgsqlCopySerializer(conn);
var npgsqlCopyIn = new NpgsqlCopyIn(cmd, conn, npgsqlCopySerializer.ToStream);

try
{
    npgsqlCopyIn.Start();
    npgsqlCopySerializer.AddInt32(300);
    npgsqlCopySerializer.AddInt32(1);
    npgsqlCopySerializer.AddString("address");
    npgsqlCopySerializer.EndRow();
    npgsqlCopySerializer.Flush();
    npgsqlCopySerializer.AddInt32(301);
    npgsqlCopySerializer.AddInt32(1);
    npgsqlCopySerializer.AddString("another\r\naddress");
    npgsqlCopySerializer.EndRow();
    npgsqlCopySerializer.Flush();
    npgsqlCopyIn.End();

    tran.Commit();
}
//catch (Exception e)
//{
//    tran.Rollback();
//    throw;
//}
finally
{
    conn.Close();
}

问题是每次 AddString()会在该方法内引发 ArgumentOutOfRangeException ,否则它将起作用。

Problem is that each time there are not allowed characters in AddString() it throws an ArgumentOutOfRangeException inside that method, otherwise it works.

例如:

npgsqlCopySerializer.AddString("another\r\naddress");

将引发异常,因为它包含换行符,该换行符在从文本格式复制(行分隔符),应该转义。

will throw the exception cause it contains a newline which has a special meaning in copy from text format (row separator) and should be escaped.

任何人都知道我可以做些什么来使其工作?我在互联网上搜索了示例,但找不到任何内容。

Anyone know what I can do to make it work? I searched for examples on the internet but I couldn't find anything.

感谢您的帮助!

推荐答案

恰好几天前,我是报告此错误的人。

I'm the person who reported the bug, coincidentally only a few days ago.

您介意发布要插入的XML吗?我已经尝试过OSM结果:
http://nominatim.openstreetmap.org/search.php?q=A7&viewbox=3.96%2C52.82%2C7.93%2C51.11&format=xml

Would you mind posting the XML you're trying to insert? I have tried it with an OSM result: http://nominatim.openstreetmap.org/search.php?q=A7&viewbox=3.96%2C52.82%2C7.93%2C51.11&format=xml

和相当大的web.config文件,它们都可以正常工作。因此,要么在提交修订时发生复制/粘贴错误,要么您尝试插入的XML与我尝试的XML不同。

And a rather large web.config file, both work flawlessly. So either I made a copy / paste error while submitting my fix, or the XML you're trying to insert is different than the XML I tried.

也:得到与以前完全相同的错误(即参数超出范围)?默认缓冲区大小仅为8k,在我的情况下还不够。

Also: do you get the exact same error as before (i.e., argument out of range)? The default buffer size is only 8k, which wasn't enough in my case.

我不知道是否有什么区别,但是我没有使用flush全部:

I don't know if it makes any difference, but I didn't use flush at all:

NpgsqlConnection Conn = new NpgsqlConnection(getPostgresConnString());
Conn.Open();
NpgsqlCopyIn copyIn = new NpgsqlCopyIn("COPY table  (col1,col2,col2)   FROM STDIN;", Conn);
copyIn.Start();
NpgsqlCopySerializer cs1 = new NpgsqlCopySerializer(pConn2);
cs1.AddString(System.IO.File.ReadAllText("C:\\test\\Web.config"));
[...]
cs1.EndRow();
cs1.Close();
copyIn.End();

这篇关于如何将NpgsqlCopyIn与NpgsqlCopySerializer一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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