现在的位置: 首页 > 综合 > 正文

Manipulating CSV Files

2019年10月06日 ⁄ 综合 ⁄ 共 987字 ⁄ 字号 评论关闭

It doesn't seem to be well known that you can easily access data in CSV files (Comma-Separated Values) using ADO.NET components. For example, you can use the OleDbConnection, OleDbCommand, and OleDbDataAdapter objects to fill a DataSet from a CSV file. You can also update the data. Also notice that it is possible to use Visual Studio's Server Explorer to visualize or edit CSV files.

All you need is to use the Jet engine thanks to a connection string like the following one:

"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/TxtFilesFolder/;Extended Properties='text;HDR=Yes;FMT=Delimited'"

Note: "HDR=Yes" indicates that the first row contains columnnames, not data (source: connectionstrings.com).

Once you have a connection to a directory, you can refer to files as tables as follows:

"SELECT * FROM data.txt"

Hint: Think about JOINs if you need them...

So, why not use ADO.NET's power to access CSV files instead of coding this all by yourself?

Update: this post seems very popular, so I guess it's useful that I add two links about the Schema.ini file for more information:

抱歉!评论已关闭.