By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

Importing from a csv file.

P: 2

I am trying to import from a csv file using the ole jet engine. All seems to work fine.

The issue I am having though is if someone has a single quote char in the title of the file eg matt's file.csv. or they have muliple periods in the file name eg Jim.File.csv

The way I am doing it is -


oledbConnection conn = new oledbconnection();
conn.ConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Da ta Source={0};Extended Properties={1}Text;Excel 8.0;HDR=No;{2}", System.IO.Path.GetDirectoryName(FileName), (char)34, (char)34);;
string CommandText = String.Format("Select * from {0}", System.IO.Path.GetFullPath(FileName));

oledbdataadapter da = new oledbdataadatper(CommandText, conn);
DataTable dt = new DataTable();

it crashes right there. saying Syntax error in from clause.

One other issue that this same code has is when someone has a csv with the """blah, blah""" which should display "blah, blah" when i grab the string. but it just crashes with another issue stating Syntax error in from clause.

By the way for the first issue. I have already tried separately building the path then adding double quotes around the file name, I have also tried doing a filename.replace("'", "''") and that has not worked either. As well I also tried putting double quotes around the entire path and file name eg("C:\\users\\test.test.csv")
Apr 24 '08 #1
Share this Question
Share on Google+
1 Reply

P: 2
Just wanted to give an update. the issue with the single quote in the file name is easily resolved by putting a [ ] around the table name
eg string test = String.Format("Select * from [{0}]", System.IO.Path.GetFileName(FileName));
Apr 24 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.