Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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();



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
Posted
Comments
[no name] 27-May-14 9:08am    
Validate the filename for invalid characters and length before you try and open it.
dielkristen 27-May-14 9:43am    
I am novice..can u please suggest me how to do validation.I din't find anything.
[no name] 27-May-14 14:05pm    
Open Google and search for "valid filename characters". And pay Attention you read the one which mach your environement.

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.
 
Share this answer
 
v2
Comments
[no name] 27-May-14 14:03pm    
5. Especially since you have made the effort to test this.
Richard MacCutchan 28-May-14 3:11am    
Testing was easy since I already had a sample app that I use for self-study.
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).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900