如何以编程方式基于SQL查询导出csv文件 [英] How to export a csv file based on a SQL query programatically

查看:379
本文介绍了如何以编程方式基于SQL查询导出csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是一个SQL专家,所以这可能是一个非常明显的事情,对于那些谁是专家。无论如何,我创建了一个应用程序,必须生成基于SQL查询的csv文件。在阅读有关如何从SQL导出CSV文件的互联网提示时,他们都引用您使用Management Studio,右键单击查询结果并使用Save-AS技术。这对于我的目的不实用,因为CSV文件必须在我的应用程序中按下按钮时生成。我希望(和期望)能够通过查询或存储过程,但我没有看到SQL 2008支持这一点。

I am not a SQL expert, so this may be a really obvious thing to those of you who are experts. Anyway, I have created an application that has to generate a csv file based on a SQL Query. In reading the internet tips on how to export a CSV file from SQL they all refer you to using Management Studio, right-click on the query results and use the Save-AS technique. That will not practical for my purposes as the CSV file has to generated at the push of a button within my application. I was hoping (and expected) to be able to do it with a pass through query or stored procedure, but I'm not seeing that SQL 2008 supports this. Any help would be very much appreciated!

推荐答案

答案将根据您的应用程序所用的语言而有所不同, C#为例。一种常见的方法是从sql查询中填充数据集,然后使用循环来遍历数据集以生成csv。

The answer will vary depending on the language that your application is written in but to use C# as an example. A common way is to populate a dataset from the sql query and then burn through the dataset using loops to generate the csv.

这里是一个来自interweb的方法的示例。

Here is an example of that approach from the interweb.

http://www.diaryofaninja.com/blog/2009/12/16/c-convert-dataset-to-csv

这里是使用VB的另一个例子

Here is another example using VB

http://www.vbnettutorial.net/?Id=119&Desc=Export-CSV-from-Dataset

数据的复杂性可能需要你喜欢...(例如,你的数据有双引号,coma的二进制数据

The complexity of the data may require that you get fancy... (for example does your data have double quotes, coma's, binary data???

这篇关于如何以编程方式基于SQL查询导出csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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