PostgreSQL:将查询结果保存到文件中并获取保存的行数,windows / odbc [英] PostgreSQL: Save query results to file and get number of saved rows, windows/odbc

查看:163
本文介绍了PostgreSQL:将查询结果保存到文件中并获取保存的行数,windows / odbc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将查询结果自动保存到文本文件,而不使用ODBC Windows连接在VB.NET中遍历阅读器对象。
但是我找不到方法!

I am trying to save query results to text file automatically, without looping through reader object in VB.NET with using ODBC windows connection. But I can't find how!

到目前为止,我一直在尝试:

That's what I try so far:

mCmd = New OdbcCommand( _
"SELECT my_id FROM " & myTable & " WHERE myflag='1' \o 'c:/a_result.txt'", mCon)
n = mCmd.ExecuteNonQuery

但这根本不起作用。
请咨询或代码示例如何获取它。

But that don't work at all. Please advice or code example how to get it.

然后第二个...

最好将结果保存到文本我在变量'n'中得到了许多保存的行。
目前,我只得到0或1,取决于查询是否成功。

And second...
It will be ideally that with saving results to text I get a number of saved rows in variable 'n'. As for now I get only 0 or 1 depends if query was successful or not.

编辑:

经过一番战斗,我发现

到txt文件:


After some fighting I found a way for do this with more or less success.
To txt file:

mCmd = New OdbcCommand( _
"COPY (SELECT my_id FROM " & myTable & " WHERE myFlag='1' " & _
"ORDER BY my_id) TO 'c:/a_result.txt' DELIMITER AS '|'", mCon)

到csv文件:

mCmd = New OdbcCommand( _
"COPY (SELECT my_id FROM " & myTable & " WHERE myFlag='1' " & _
"ORDER BY my_id) TO 'c:/a_result.csv' WITH CSV", mCon)

可以,但是我无法转义引号和'\',所以我在输出文件中出现了双重符号。

如果有经验的人知道如何实现转义和更改csv文件的定界符,我会很高兴

That works, but I am not able to escape quotes and '\' so I got double signs in output file.
If someone with experience know how to achieve escaping and changing delimiter for csv files I would be glad to see it on given example.

查询后的变量'n'包含许多导出的行。

Variable 'n' after query contain a number of exported rows.

推荐答案

\o 序列是 psql 元命令。这意味着它是 psql 的功能。如果您需要此功能,则必须在客户端中实现。不过,这很容易。

The \o sequence is a psql meta-command. That means it is a feature of psql. If you want this functionnality you will have to implement it in your client. It is very easy though.

这篇关于PostgreSQL:将查询结果保存到文件中并获取保存的行数,windows / odbc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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