当文件名太长时,如何使用oledb reader读取.csv文件? [英] How to read a .csv file using oledb reader when name of the file is too long ?

查看:171
本文介绍了当文件名太长时,如何使用oledb reader读取.csv文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

oleDbConnectionStringBuilder sbConnection = new OleDbConnectionStringBuilder();
sbConnection.DataSource = System.IO.Path.GetDirectoryName(txt_selectDirectory.Text);
sbConnection.Provider = "Microsoft.ACE.OLEDB.12.0";
String strExtendedProperties = String.Empty;
strExtendedProperties = String.Format("Text;IMEX=1;FMT=Delimited", "NO");
sbConnection.Add("Extended Properties", strExtendedProperties);
String filename = System.IO.Path.GetFileName(txt_selectDirectory.Text);
OleDbDataReader datareader;
System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection(sbConnection.ToString());
System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand(String.Format("Select * FROM [{0}]", filename), connection);
connection.Open();
command.CommandTimeout = 0;
datareader = command.ExecuteReader();




文件546906592003-bla-bla-bla-2014-05.csv
都没有例外 但是当文件名很长时就像546906592003-bla-bla-bla-bla-bla-bla-bla-bla-bla-bla-bla-bla-bla-bla-bla-bla-bla-bla-bla-bla-bla-bla-bla-2014-05 .csv
引发异常:
文件名不是有效的名称.确保它不包含无效字符或标点符号,并且时间不要太长.




No exception is comming for file 546906592003-bla-bla-bla-2014-05.csv
But when file name is long like 546906592003-bla-bla-bla-bla-bla-bla-blabla-bla-blabla-bla-blabla-bla-blabla-bla-blabla-bla-blabla-bla-bla-2014-05.csv
Exception is thrown:
file name is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long

推荐答案

根据我的测试,看来OLEDB的文件名受到限制最多为64个字符,其中包括.csv扩展名.
According to my tests, it would appear that a filename for OLEDB is restricted to a total of 64 characters, this includes the .csv extension.


首先检查文件名,如果文件名太长,可以暂时将其更改为较短的文件名(如果文件名不是太短)较大的文件,您可能需要一个临时副本),然后将其转换回(或删除该临时文件).
first check the filename, if it is too long you could temporarily change it to something shorter (if the file is not too large you might want a temporary copy) and then convert it back (or delete the temporary file).


这篇关于当文件名太长时,如何使用oledb reader读取.csv文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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