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

查看:185
本文介绍了.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来处理转义,因为Reed指出,逗号。 这里是一个例子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天全站免登陆