c#读取csv文件不给出有效路径 [英] c# reading csv file gives not a valid path

查看:895
本文介绍了c#读取csv文件不给出有效路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法使用以下连接字符串读取.csv文件:

I can't seem to read a .csv file using the following connection string:

var fileName = string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, "Uploads\\countrylist.csv");
string connectionString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}; Extended Properties=""text;HDR=YES;FMT=Delimited""", fileName);
OleDbConnection oledbConn = new OleDbConnection(connectionString);
oledbConn.Open();

出现以下错误:


'D:\arrgh\arrgh\Uploads\countrylist.csv'不是有效的路径。请确保路径名称拼写正确,并且已连接到文件所在的服务器。

'D:\arrgh\arrgh\Uploads\countrylist.csv' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.

我验证了文件是有。这里发生了什么?

I verified that the file is there. What is happening here?

推荐答案

好的,我挖了一点,似乎我的连接字符串是错误的。对于CSV文件,您不指定实际的文件名,而是指定实际的文件名,但不是指定它所属的目录。

Ok, I dug a little further and it seems that my connection string is wrong. With CSV files, you don't specify the actual file name but the directory where it belongs, eg.

var fileName = string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, "Uploads\\");
string connectionString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}; Extended Properties=""text;HDR=YES;FMT=Delimited""", fileName);
OleDbConnection oledbConn = new OleDbConnection(connectionString);
oledbConn.Open();
var cmd = new OleDbCommand("SELECT * FROM [countrylist.csv]", oledbConn);

并在SelectCommand中指定文件名。什么奇怪的做法。它现在为我工作。

And you specify the filename in the SelectCommand. What a strange way of doing it. It's working for me now.

这篇关于c#读取csv文件不给出有效路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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