<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Deborah's Developer MindScape : Data, text file</title><link>http://msmvps.com/blogs/deborahk/archive/tags/Data/text+file/default.aspx</link><description>Tags: Data, text file</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Reading Fixed Length Files</title><link>http://msmvps.com/blogs/deborahk/archive/2009/08/25/reading-fixed-length-files.aspx</link><pubDate>Tue, 25 Aug 2009 20:34:15 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1718162</guid><dc:creator>Deborah Kurata</dc:creator><slash:comments>15</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/deborahk/rsscomments.aspx?PostID=1718162</wfw:commentRss><comments>http://msmvps.com/blogs/deborahk/archive/2009/08/25/reading-fixed-length-files.aspx#comments</comments><description>&lt;p&gt;There may be times that you need to read fixed length files into your application. For example, you obtain output from a legacy system or other application in a fixed length text file format, and you need to read and use that data in your application.&lt;/p&gt;  &lt;p&gt;NOTE: For more information on fixed length files, see &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/08/25/text-files.aspx"&gt;this link&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;.NET provides several techniques for reading text files. This post focuses on how to read a fixed length text file into a DataTable.&lt;/p&gt;  &lt;p&gt;You may find it very useful to read your text file into a DataTable, &lt;strong&gt;whether or not you plan to use a database&lt;/strong&gt;. Reading a text file into a DataTable not only saves you a significant amount of string manipulation coding, it also makes it easy to access the imported data from within your application. &lt;/p&gt;  &lt;p&gt;For example, you can use binding to bind the resulting DataTable to a grid or other controls. You can use Linq to DataTables like in &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/23/linq-sorting-a-datatable.aspx" target="_blank"&gt;this example&lt;/a&gt; to manipulate the resulting data. All of the features of the DataTable are then available to you.&lt;/p&gt;  &lt;p&gt;BIG NOTE: &lt;strong&gt;Many developers have ignored this technique because one look at the code and the developer assumed it is somehow associated with a database, it is NOT.&lt;/strong&gt; This is referring to in-memory DataTable objects.&lt;/p&gt;  &lt;p&gt;For this example, the text file appears as follows:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;000001&amp;#160; Baggins&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Bilbo&amp;#160;&amp;#160;&amp;#160;&amp;#160; 20090811      &lt;br /&gt;000002&amp;#160; Baggins&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Frodo&amp;#160;&amp;#160;&amp;#160;&amp;#160; 20090801       &lt;br /&gt;000003&amp;#160; Gamgee&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Samwise&amp;#160;&amp;#160; 20090820       &lt;br /&gt;000004&amp;#160; Cotton&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Rosie&amp;#160;&amp;#160;&amp;#160;&amp;#160; 20090821&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Notice several things about this file:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;The columns are a fixed width. &lt;/li&gt;    &lt;li&gt;There is no header row that provides the column names. You could add column headers here if desired. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;The first step in reading the file is to define a schema.ini file that defines the column widths. The file must follow these specifications:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The file &lt;em&gt;must&lt;/em&gt; be called &lt;strong&gt;schema.ini&lt;/strong&gt;. &lt;/li&gt;    &lt;li&gt;The file &lt;em&gt;must&lt;/em&gt; exist in the same directory as the text file. &lt;/li&gt;    &lt;li&gt;The file &lt;em&gt;must&lt;/em&gt; be in ANSI format. (See the note at the bottom of this post for information on saving a file to ANSI format.) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The contents of the schema.ini file for the example above is shown below:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;[testFixed.txt]      &lt;br /&gt;ColNameHeader=False       &lt;br /&gt;Format=FixedLength       &lt;br /&gt;DateTimeFormat=yyyymmdd       &lt;br /&gt;Col1=CustomerId Text Width 6       &lt;br /&gt;Col2=LastName Text Width 22       &lt;br /&gt;Col3=FirstName Text Width 10       &lt;br /&gt;Col4=LastUpdateDate DateTime Width 8&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The first line of the file is always the name of the associated text file enclosed in square brackets ([ ]).&lt;/p&gt;  &lt;p&gt;The next set of lines define basic attributes of the text file:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;ColNameHeader&lt;/strong&gt;: In this case, there is no column header in the text file, so this property is set to false. The system will assume that the first line of the text file is the header unless you specify otherwise. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Format&lt;/strong&gt;: In this case, the format is &lt;strong&gt;FixedLength&lt;/strong&gt;. The system will assume comma delimited unless you specify otherwise. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;DateTimeFormat&lt;/strong&gt;: If you have a date in your file, you can specify the format here. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The last set of lines defines each column in the text file. The format of these lines are as follows:&lt;/p&gt;  &lt;p&gt;Col&lt;em&gt;x&lt;/em&gt;=&lt;em&gt;ColumnName&lt;/em&gt; &lt;em&gt;ColumnType&lt;/em&gt; Width &lt;em&gt;ColumnWidth&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;See &lt;a href="http://msdn.microsoft.com/en-us/library/ms709353(VS.85).aspx" target="_blank"&gt;this link&lt;/a&gt; for more information on the contents of the schema.ini file.&lt;/p&gt;  &lt;p&gt;You can then read the file using the following code.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;string fileName = &amp;quot;testFixed.txt&amp;quot;;      &lt;br /&gt;string dirName = Path.GetDirectoryName(Application.ExecutablePath);       &lt;br /&gt;DataTable dt; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;using (OleDbConnection cn =      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; new OleDbConnection(@&amp;quot;Provider=Microsoft.Jet.OleDb.4.0;&amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;Data Source=&amp;quot; + dirName + &amp;quot;;&amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;Extended Properties=\&amp;quot;Text;\&amp;quot;&amp;quot;))       &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; // Open the connection       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; cn.Open(); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; // Set up the adapter      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; using (OleDbDataAdapter adapter =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new OleDbDataAdapter(&amp;quot;SELECT * FROM &amp;quot; + fileName, cn))       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dt = new DataTable(&amp;quot;Customer&amp;quot;);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; adapter.Fill(dt);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Dim fileName As String = &amp;quot;testCSV.txt&amp;quot;      &lt;br /&gt;Dim dirName As String = _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Path.GetDirectoryName(Application.ExecutablePath)       &lt;br /&gt;Dim dt As DataTable &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Using cn As New OleDbConnection(&amp;quot;Provider=Microsoft.Jet.OleDb.4.0;&amp;quot; &amp;amp; _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;Data Source=&amp;quot; &amp;amp; dirName &amp;amp; &amp;quot;;&amp;quot; &amp;amp; _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;Extended Properties=&amp;quot;&amp;quot;Text;&amp;quot;&amp;quot;&amp;quot;)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Open the connection       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; cn.Open() &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Set up the adapter      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Using adapter As New OleDbDataAdapter( _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;SELECT * FROM &amp;quot; &amp;amp; fileName, cn)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dt = New DataTable(&amp;quot;Customer&amp;quot;)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; adapter.Fill(dt)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Using       &lt;br /&gt;End Using&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;This code starts by declaring variables to hold the text file name, directory containing the file and the resulting DataTable.&lt;/p&gt;  &lt;p&gt;This technique only works with a standard set of file name extensions (see the NOTE at the end of this post). The file can reside in any directory. In this example, the file resides in the same directory where the application is executed. But this is not a requirement. &lt;/p&gt;  &lt;p&gt;The first using statement in the example code sets up the connection string for connecting to the directory. It sets the &lt;strong&gt;Provider&lt;/strong&gt; property to use the Microsoft.Jet.OleDb provider. The &lt;strong&gt;Data Source&lt;/strong&gt; property defines the directory containing the text file. The &lt;strong&gt;Extended Properties&lt;/strong&gt; define that the file will be Text (&amp;quot;Text&amp;quot;). The Extended Properties must be within quotes, so double-quotes (VB) or slash quote (C#) are used to escape the quotes.&lt;/p&gt;  &lt;p&gt;If a schema.ini file exists in the directory defined as the data source and has a bracketed entry with the text file name, that .ini file is used to determine any other extended properties. So no other extended properties are defined in the connection string itself.&lt;/p&gt;  &lt;p&gt;The code then opens the connection, thereby opening the file and the associated schema.ini file. Since this code is in a using statement, the files are automatically closed at the end of the using block.&lt;/p&gt;  &lt;p&gt;The second using statement sets of the DataAdapter by defining a Select statement and the open connection. The Select statement selects all of the information from a specific file as defined by the fileName variable.&lt;/p&gt;  &lt;p&gt;The code then creates the DataTable, giving the table a name. In this example, the table name is &amp;quot;Customer&amp;quot;.&lt;/p&gt;  &lt;p&gt;Finally, it uses the Fill method of the TableAdapter to read the data from the text file into the DataTable.&lt;/p&gt;  &lt;p&gt;Using the technique detailed &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/10/dal-datatable-visualizer.aspx"&gt;here&lt;/a&gt;, you can view the resulting DataTable. The column headings were defined by the header in the text file. If you don&amp;#39;t have a header, the columns will be giving a default name.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/deborahk.metablogapi/2148.image_5F00_605ACCCC.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" border="0" alt="image" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/deborahk.metablogapi/7357.image_5F00_thumb_5F00_30338209.png" width="491" height="231" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Note how the date in the above screen shot appears as a standard date column.&lt;/p&gt;  &lt;p&gt;You can then access the data in the table as you access any other DataTable. For example:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;foreach (DataRow dr in dt.Rows)      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Debug.Print(&amp;quot;{0}: {1}, {2} LastUpdated: {3}&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dr[&amp;quot;CustomerId&amp;quot;],       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dr[&amp;quot;LastName&amp;quot;],       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dr[&amp;quot;FirstName&amp;quot;],       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dr[&amp;quot;LastUpdateDate&amp;quot;]); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;For Each dr As DataRow In dt.Rows      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Debug.Print(&amp;quot;{0}: {1}, {2} LastUpdated: {3}&amp;quot;, _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dr(&amp;quot;CustomerId&amp;quot;), _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dr(&amp;quot;LastName&amp;quot;), _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dr(&amp;quot;FirstName&amp;quot;), _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dr(&amp;quot;LastUpdateDate&amp;quot;))       &lt;br /&gt;Next&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;:&lt;/p&gt;  &lt;p&gt;By default, this technique only works with .txt, .csv, .tab, and .asc file extensions. If your file name has a different extension, you can either change the extension in your code before reading the file, or you can update the Extensions key in following registry setting: &lt;/p&gt;  &lt;p&gt;HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Text &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;:&lt;/p&gt;  &lt;p&gt;By default, this technique assumes you are working with ANSI text files. If that is not the case, you can update the CharacterSet key in the same registry setting: &lt;/p&gt;  &lt;p&gt;HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Text &lt;/p&gt;  &lt;p&gt;Though this is not recommended. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;VERY IMPORTANT NOTE&lt;/strong&gt;: &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;If you test this sample code by creating a text file with Visual Studio, the resulting text file will be in UTF-8 format. You need to save the file into ANSI format.&lt;/strong&gt; The easiest way I found to do this is detailed below.&lt;/p&gt;  &lt;p&gt;Adding a Text File to your Project: &lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Right-click on your project in Visual Studio. &lt;/li&gt;    &lt;li&gt;Select Add | New Item from the context menu. &lt;/li&gt;    &lt;li&gt;Pick Text File from the available templates and click Add. &lt;/li&gt;    &lt;li&gt;Type in the data for the test file or paste in the text from the example at the top of this post. &lt;/li&gt;    &lt;li&gt;Save the file within Visual Studio. This creates a UTF-8 formatted file. &lt;/li&gt;    &lt;li&gt;If you plan to use the directory of the executing application, set the &lt;strong&gt;Copy to Output Directory&lt;/strong&gt; to &lt;strong&gt;Copy always&lt;/strong&gt; in the properties window for the file. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Converting the resulting UTF-8 file to ANSI format:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Right-click on the file and select &lt;strong&gt;Open With&lt;/strong&gt; … &lt;/li&gt;    &lt;li&gt;Select &lt;strong&gt;Notepad&lt;/strong&gt;. &lt;/li&gt;    &lt;li&gt;Select &lt;strong&gt;File | Save As&lt;/strong&gt;. &lt;/li&gt;    &lt;li&gt;Set the Encoding to &lt;strong&gt;ANSI&lt;/strong&gt; and click &lt;strong&gt;Save&lt;/strong&gt;. &lt;/li&gt; &lt;/ol&gt; Enjoy!   &lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1718162" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/deborahk/archive/tags/VB/default.aspx">VB</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/Data/default.aspx">Data</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/DataTable/default.aspx">DataTable</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/text+file/default.aspx">text file</category></item><item><title>Reading Comma Delimited Files</title><link>http://msmvps.com/blogs/deborahk/archive/2009/08/25/reading-comma-delimited-files.aspx</link><pubDate>Tue, 25 Aug 2009 19:14:24 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1718146</guid><dc:creator>Deborah Kurata</dc:creator><slash:comments>13</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/deborahk/rsscomments.aspx?PostID=1718146</wfw:commentRss><comments>http://msmvps.com/blogs/deborahk/archive/2009/08/25/reading-comma-delimited-files.aspx#comments</comments><description>&lt;p&gt;There may be times that you need to read comma separated value (CSV) files into your application. For example, you obtain output from a legacy system or other application in a comma delimited text file format, and you need to read and use that data in your application.&lt;/p&gt;  &lt;p&gt;NOTE: For more information on delimited files, see &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/08/25/text-files.aspx" target="_blank"&gt;this link&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;.NET provides several techniques for reading text files. This post focuses on how to read a comma delimited text file into a DataTable.&lt;/p&gt;  &lt;p&gt;You may find it very useful to read your text file into a DataTable, &lt;strong&gt;whether or not you plan to use a database&lt;/strong&gt;. Reading a text file into a DataTable not only saves you a significant amount of string manipulation coding, it also makes it easy to access the imported data from within your application. &lt;/p&gt;  &lt;p&gt;For example, you can use binding to bind the resulting DataTable to a grid or other controls. You can use Linq to DataTables like in &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/23/linq-sorting-a-datatable.aspx" target="_blank"&gt;this example&lt;/a&gt; to manipulate the resulting data. All of the features of the DataTable are then available to you.&lt;/p&gt;  &lt;p&gt;BIG NOTE: &lt;strong&gt;Many developers have ignored this technique because one look at the code and the developer assumed it is somehow associated with a database, it is NOT.&lt;/strong&gt; This is referring to in-memory DataTable objects.&lt;/p&gt;  &lt;p&gt;For this example, the text file appears as follows:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;CustomerId, LastName, FirstName, LastUpdateDate      &lt;br /&gt;1,&amp;#160; Baggins, Bilbo, 20090811&amp;#160; &lt;br /&gt;2,&amp;#160; Baggins, Frodo, 20090801&amp;#160; &lt;br /&gt;3,&amp;#160; Gamgee,&amp;#160; Samwise, 20090820&amp;#160; &lt;br /&gt;4,&amp;#160; Cotton,&amp;#160; Rosie, 20090821&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Notice several things about this file:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;It is a comma separated value (CSV) file. &lt;/li&gt;    &lt;li&gt;The first line provides the column names. This is optional. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;You can read this text file into a DataTable using OleDb as follows.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;string fileName = &amp;quot;testCSV.txt&amp;quot;;      &lt;br /&gt;string dirName = Path.GetDirectoryName(Application.ExecutablePath);       &lt;br /&gt;DataTable dt; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;using (OleDbConnection cn =      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; new OleDbConnection(@&amp;quot;Provider=Microsoft.Jet.OleDb.4.0;&amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;Data Source=&amp;quot; + dirName + &amp;quot;;&amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;Extended Properties=\&amp;quot;Text;HDR=Yes;FMT=Delimited\&amp;quot;&amp;quot;))       &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; // Open the connection       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; cn.Open(); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; // Set up the adapter      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; using (OleDbDataAdapter adapter =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new OleDbDataAdapter(&amp;quot;SELECT * FROM &amp;quot; + fileName, cn))       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dt = new DataTable(&amp;quot;Customer&amp;quot;);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; adapter.Fill(dt);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Dim fileName As String = &amp;quot;testCSV.txt&amp;quot;      &lt;br /&gt;Dim dirName As String = _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Path.GetDirectoryName(Application.ExecutablePath)       &lt;br /&gt;Dim dt As DataTable &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Using cn As New OleDbConnection(&amp;quot;Provider=Microsoft.Jet.OleDb.4.0;&amp;quot; &amp;amp; _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;Data Source=&amp;quot; &amp;amp; dirName &amp;amp; &amp;quot;;&amp;quot; &amp;amp; _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;Extended Properties=&amp;quot;&amp;quot;Text;HDR=Yes;FMT=Delimited&amp;quot;&amp;quot;&amp;quot;)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Open the connection       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; cn.Open() &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;#39; Set up the adapter      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Using adapter As New OleDbDataAdapter( _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;SELECT * FROM &amp;quot; &amp;amp; fileName, cn)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dt = New DataTable(&amp;quot;Customer&amp;quot;)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; adapter.Fill(dt)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End Using       &lt;br /&gt;End Using&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;This code starts by declaring variables to hold the text file name, directory containing the file and the resulting DataTable.&lt;/p&gt;  &lt;p&gt;This technique only works with a standard set of file name extensions (see the NOTE at the end of this post). The file can reside in any directory. In this example, the file resides in the same directory where the application is executed. But this is not a requirement. &lt;/p&gt;  &lt;p&gt;The first using statement in the example code sets up the connection to the directory. It sets the &lt;strong&gt;Provider&lt;/strong&gt; property to use the Microsoft.Jet.OleDb provider. The &lt;strong&gt;Data Source&lt;/strong&gt; property defines the directory containing the text file. The &lt;strong&gt;Extended Properties&lt;/strong&gt; define that the file will be Text (&amp;quot;Text&amp;quot;), it has a header (HDR=Yes), and it is in a delimited file format (FRM=Delimited). The Extended Properties must be within quotes, so double-quotes (VB) or slash quote (C#) are used to escape the included quotes.&lt;/p&gt;  &lt;p&gt;The code then opens the connection, thereby opening the file. Since this code is in a using statement, the file is automatically closed at the end of the using block.&lt;/p&gt;  &lt;p&gt;The second using statement sets of the DataAdapter by defining a Select statement and the open connection. The Select statement selects all of the information from a specific file as defined by the fileName variable.&lt;/p&gt;  &lt;p&gt;The code then creates the DataTable, giving the table a name. In this example, the table name is &amp;quot;Customer&amp;quot;.&lt;/p&gt;  &lt;p&gt;Finally, it uses the Fill method of the TableAdapter to read the data from the text file into the DataTable.&lt;/p&gt;  &lt;p&gt;Using the technique detailed &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/10/dal-datatable-visualizer.aspx" target="_blank"&gt;here&lt;/a&gt;, you can view the resulting DataTable. The column headings were defined by the header in the text file. If you don&amp;#39;t have a header, the columns will be giving a default name.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/deborahk.metablogapi/5314.image_5F00_14DD1EA8.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" border="0" alt="image" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/deborahk.metablogapi/2158.image_5F00_thumb_5F00_64B5D3E4.png" width="488" height="230" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;You can then access the data in the table as you access any other DataTable. For example:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;foreach (DataRow dr in dt.Rows)      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Debug.Print(&amp;quot;{0}: {1}, {2} LastUpdated: {3}&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dr[&amp;quot;CustomerId&amp;quot;],       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dr[&amp;quot;LastName&amp;quot;],       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dr[&amp;quot;FirstName&amp;quot;],       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dr[&amp;quot;LastUpdateDate&amp;quot;]); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;For Each dr As DataRow In dt.Rows      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Debug.Print(&amp;quot;{0}: {1}, {2} LastUpdated: {3}&amp;quot;, _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dr(&amp;quot;CustomerId&amp;quot;), _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dr(&amp;quot;LastName&amp;quot;), _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dr(&amp;quot;FirstName&amp;quot;), _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dr(&amp;quot;LastUpdateDate&amp;quot;))       &lt;br /&gt;Next&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;:&lt;/p&gt;  &lt;p&gt;By default, this technique only works with .txt, .csv, .tab, and .asc file extensions. If your file name has a different extension, you can either change the extension in your code before reading the file, or you can update the Extensions key in following registry setting: &lt;/p&gt;  &lt;p&gt;HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Text &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;:&lt;/p&gt;  &lt;p&gt;By default, this technique assumes you are working with ANSI text files. If that is not the case, you can update the CharacterSet key in the same registry setting: &lt;/p&gt;  &lt;p&gt;HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Text &lt;/p&gt;  &lt;p&gt;Though this is not recommended. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;VERY IMPORTANT NOTE&lt;/strong&gt;: &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;If you test this sample code by creating a text file with Visual Studio, the resulting text file will be in UTF-8 format. You need to save the file into ANSI format.&lt;/strong&gt; The easiest way I found to do this is detailed below.&lt;/p&gt;  &lt;p&gt;Adding a Text File to your Project: &lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Right-click on your project in Visual Studio. &lt;/li&gt;    &lt;li&gt;Select Add | New Item from the context menu. &lt;/li&gt;    &lt;li&gt;Pick Text File from the available templates and click Add. &lt;/li&gt;    &lt;li&gt;Type in the data for the test file or paste in the text from the example at the top of this post. &lt;/li&gt;    &lt;li&gt;Save the file within Visual Studio. This creates a UTF-8 formatted file. &lt;/li&gt;    &lt;li&gt;If you plan to use the directory of the executing application, set the &lt;strong&gt;Copy to Output Directory&lt;/strong&gt; to &lt;strong&gt;Copy always&lt;/strong&gt; in the properties window for the file. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Converting the resulting UTF-8 file to ANSI format:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Right-click on the file and select &lt;strong&gt;Open With&lt;/strong&gt; … &lt;/li&gt;    &lt;li&gt;Select &lt;strong&gt;Notepad&lt;/strong&gt;. &lt;/li&gt;    &lt;li&gt;Select &lt;strong&gt;File | Save As&lt;/strong&gt;. &lt;/li&gt;    &lt;li&gt;Set the Encoding to &lt;strong&gt;ANSI&lt;/strong&gt; and click &lt;strong&gt;Save&lt;/strong&gt;. &lt;/li&gt; &lt;/ol&gt; Enjoy!   &lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1718146" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/deborahk/archive/tags/VB/default.aspx">VB</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/Data/default.aspx">Data</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/DataTable/default.aspx">DataTable</category><category domain="http://msmvps.com/blogs/deborahk/archive/tags/text+file/default.aspx">text file</category></item></channel></rss>