My original project task was pretty straight forward.
I had a number of .csv files generated by SysMon/Perfmon and needed to import them into a MS-SQL 2005 database for analysis.
As you know, the .csv file output by Perfmon has a linear format rather than a relational one.
Meaning that each record/line in the .csv file is formatted using:
"Datetime of the sample",
"\\<hostname>\<CounterClass1>\<CounterName1>",
"\\<hostname>\<CounterClass2>\<CounterName2>",
"\\<hostname>\<CounterClass3>\<CounterName3>",
"\\<hostname>\<CounterClass4>\<CounterName4>",
"\\<hostname>\<CounterClass(n...)>\<CounterName(n...)>",
So, the question became, if you have a text file that is comma delimited (or delimited by some character) and you want to open and read it using ADO.NET. What "Provider", connection string, and "Extended Properties" do you use to access a text file with?
Um...This was tricky at first, but once I found the answer, it turned out be unbelievably simple. The problem (as usual) was trying to get/find the techinical information from the manufacturer itself (Ie. Microsoft). I probably spent an entire day searching the MSDB, MS-TechNect, Support, Help files, etc., trying to find the answer.
The short answer is, Microsoft ADO.NET does NOT have a "provider" for text files. Instead, you have to use the "OLEDB" provider. Once I figured this out, it then became a battle to try and find a list of "Extended Properties" for a text file connection string. Which, as usual with Microsoft, is nowhere to be (easily) found.
So...Here's how..."
cnStr = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\MyDir\Perfmon\Files;" & _ "Extended Properties='text;HDR=YES;FMT=CSVDelimited';"
Using myCMD As New OleDb.OleDbCommand(strQuery,myCN) 'Init the reader myReader = myCMD.ExecuteReader() While myReader.Read xDate = myReader(0) xVal1 = myReader(1) xVal2 = myReader(2) xVal3 = myReader(3) xValn... = myReader(n..) 'Process data..... End While End Using
Voila! You can now use ADO.NET/VB.NET to read and process a text file with a DataReader (or DataAdapter/DataSet combo..)
BEWARE! Depending on the file extension, you might receive the following error when you attempt to open and read a text file:
For security reasons, the latest service pack for Jet (by default) allows only text files with the following extensions to be imported:
I hope this helps you in your endeavors. Naturally, MicroSoft was a hinderance by failing to provided any easily reachable examples or documentation. But then, when you are a unscrupulous monopoly, what does customer service matter?
Here is a link to the list of extended properties I've discovered so far: Extended Properties