Manipulating CSV Files

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:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值