0

I'm attempting to write a connection string for a .txt file with C#.

I keep getting the error saying that my file path is invalid.

string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Users\Josh\Documents\Test.txt;Extended Properties=""text;HDR=YES;FMT=Delimited""";

The file path shouldn't be wrong. So, I'm thinking it's some other problem. Perhaps I'm missing something with the file path though.

Any suggestions would be great.

6
  • Try adding double-quotes around the path, as in "C:\Users\Josh\Documents\Test.txt" (and remove the wrong Extended Properties=""text - the text doesn't belong there). Commented Nov 3, 2012 at 4:08
  • I need to add multiple sets of double quotes to keep the path with the string. Also, should I replace "text" with anything in the Extend Propteries? Or leave it exactly blank? Commented Nov 3, 2012 at 4:49
  • The word text does not belong there at all. It should either be inside the "" or removed entirely. ConnectionStrings is a site you should bookmark if you're going to do much with ADO. Commented Nov 3, 2012 at 5:04
  • This is the string I've been referencing from the connectionstrings site. Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\txtFilesFolder\; Extended Properties="text;HDR=Yes;FMT=Fixed"; I've been using it, but I must be missing something still. This string has "text" in it, it also does not have double quotes around the file path like you suggested. Am I referencing the wrong string from the website? I'm looking in their text file section under microsoft oledb jet delimited column string Commented Nov 3, 2012 at 5:20
  • That new string has double quotes with the "text;HDR=Yes;FMT=Fixed" inside the quotes; your original one had ""text;HDR=Yes;FMT=Fixed, with the text following (after) the quotes. Can't you see the difference? If not, copy and paste each of them into a text editor, each on it's own line, and compare them character by character. The new one should be correct. Commented Nov 3, 2012 at 14:28

1 Answer 1

2

For text files the data source is a directory not an individual file see: ADODB Connection String for .csv

So the connection string should be

string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; 
Data Source=C:\Users\Josh\Documents\;Extended Properties=""text;
HDR=YES;FMT=Delimited""";

Then you would do

SELECT * FROM Test.txt

To get at the data

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.