0

Could any one help me with a connection string to a Microsoft flat file , extension txt ? I simply want to read a txt document delimited with | .

Get the data and load it into a DataTable via a DataAdapter if possible, the first row on the file should be the columns name on the DataTable, finally DataType is not important on the DataTable

Dante May Code
  • 11,177
  • 9
  • 49
  • 81
Nathan M
  • 33
  • 1
  • 2

4 Answers4

2

try this

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\filepath;Extended Properties="text;HDR=Yes;FMT=Delimited";

Something like thsi should work

  string strSql = "SELECT * FROM [" + fileName + "]"; 

  string strCSVConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFilePath + ";" + "Extended Properties='text;HDR=YES;'"; 

  // load the data from CSV to DataTable 

  OleDbDataAdapter oleda = new OleDbDataAdapter(strSql, strCSVConnString); 
  DataTable dtbCSV = new DataTable(); 
  oleda.Fill(dtbCSV); 
Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
  • Please try to put it into a piece of code or a connection string to show how you use it – Nathan M Nov 28 '12 at 13:39
  • @NathanM, please find it in edited post – Arsen Mkrtchyan Nov 28 '12 at 13:48
  • Thankx ArsenMkrt, i am now able to access the file but because it is Coulmn Delimited with | was not able to get all the columns any idea ? I am looping through the rows from the datatable to get just the row name for now and it does not get more than 2 strange names, i am using a foreach to iterate – Nathan M Nov 28 '12 at 13:57
  • please refere to this, if you want to define delimiter http://stackoverflow.com/questions/3109360/how-to-specify-the-delimiter-when-importing-csv-files-via-oledb-in-c-sharp – Arsen Mkrtchyan Nov 28 '12 at 13:59
  • I've indeed checked the link but i think i am having the same issue which apparently seems to have no answer – Nathan M Nov 28 '12 at 14:09
1

Have a look at http://connectionstrings.com/textfile

Example connection string:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\txtFilesFolder\;
Extended Properties="text;HDR=Yes;FMT=Delimited";
cjk
  • 45,739
  • 9
  • 81
  • 112
  • Thankx guys but how do you put call it or put in a connection string, just starting with C# so i'm far from beeing a guru as you could already see :) – Nathan M Nov 28 '12 at 13:29
  • @NathanM a connection string is just that - a `string`. Assign the string that cjk provided (with the correct path) to the `ConnectionString` property. – Jon B Nov 28 '12 at 13:40
  • Yes JonB but getting the error message " Could not find installed ISAM " – Nathan M Nov 28 '12 at 13:44
  • @NathanM - including an error message when you ask for help is crucial to get good answers. – Jon B Nov 28 '12 at 13:50
  • 1
    http://stackoverflow.com/a/512187/14357 – spender Nov 28 '12 at 13:55
1

How about taking advantage of what .Net has to offer:

Reference Microsoft.VisualBasic and you can use TextFieldParser

using (var parser =
    new TextFieldParser(@"c:\data.csv")
        {
            TextFieldType = FieldType.Delimited,
            Delimiters = new[] { "," }
        })
{
    while (!parser.EndOfData)
    {
        string[] fields;
        fields = parser.ReadFields();
        //go go go!
    }
}
spender
  • 117,338
  • 33
  • 229
  • 351
  • Is it not possible to do more simple like calling a DataAdapter and filling a DataTable ? – Nathan M Nov 28 '12 at 13:32
  • Arrays of fields are really as simple as it can possibly get. DataAdapters and Tables are not simple. IIRC microsoft.jet.oledb really isn't that portable, and is only assured to work under 32bit processes (i.e. the x64 driver isn't installed on some x64 operating systems). Seems to me that invoking a driver to do this is painfully long winded and slightly brittle. – spender Nov 28 '12 at 13:47
0

I've found what was causing the probleme. On MS website site they talk about a schema.ini file that should be able to take care of Delimited files bla bla bla , but i have noticed that once you put all your settings in the registry it takes over. So once i change Format in the Registry ArsenMkrt code and connection strings works, i did adjustment to my need and it's all good.

Thank to all of you and a very big thank to ArsenMkrt.

I love C#

Nathan M
  • 33
  • 1
  • 2