可以.csv文件作为Visual Studio 2008中的数据源? [英] Can a .csv file be used as a data source in Visual Studio 2008?

查看:123
本文介绍了可以.csv文件作为Visual Studio 2008中的数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的C#和Visual Studio。我正在写一个小程序来读取.csv文件,然后写读到SQL Server数据库表中的记录。
我可以手动解析.csv文件,但我想知道是否有可能以某种方式形容.csv文件到Visual Studio这样我可以用它作为数据源?我应该指出,前两行.csv文件包含标题信息,并以下线的实际逗号分隔的数据。

I'm pretty new to C# and Visual Studio. I'm writing a small program that will read a .csv file and then write the records read to a SQL Server database table. I can manually parse the .csv file, but I was wondering if it is possible to somehow "describe" the .csv file to Visual Studio so that I can use it as a data source? I should mention that the first two lines in the .csv file contain header information and the following lines are the actual comma-delimited data.

另外,我要指出,这程序是没有用户界面的一个独立的控制台程序。

Also, I should mention that this program is a stand-alone console program with no user interface.

推荐答案

这是使用LINQ力量的一个很好的例子。的下面是对如何做到这一点的例子的快速参考

This is a great example of using the power of LINQ. Here's a quick reference with an example of how to do it.

跑下来是这样的。您可以在CSV读一个字符串数组,然后使用LINQ查询针对该集合。里德虽然所指出的,你必须编写你的周围标题行,因为它会甩开你的查询。

The run down is this. You can read in your CSV to a string array, then use LINQ to query against that collection. As Reed points out though, you'll have to code around your header line, as it will throw off your query.

您也可以使用使用TextFieldParser过处理逃逸逗号。 这里的上thinqlinq一个例子使用使用TextFieldParser来解析文件和LINQ查询得到的结果。它甚至有一个单元测试,以确保逃脱逗号处理。

You can also use the TextFieldParser too to handle escaping commas. Here's an example on thinqlinq that uses the TextFieldParser to parse the file, and a LINQ query to get the results. It even has a unit test to make sure escaped commas are handled.

这篇关于可以.csv文件作为Visual Studio 2008中的数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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