Click here to Skip to main content
15,896,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have below code to parse CSV file
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.IO;
using System.Data.OleDb;
using System.Globalization;

namespace ReadCSVFile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string path =@"D:\csvfile.csv";
            DataTable de = GetDataTableFromCsv(path, false);
        }

        static DataTable GetDataTableFromCsv(string path, bool isFirstRowHeader)
        {
            string header = isFirstRowHeader ? "Yes" : "No";

            string pathOnly = Path.GetDirectoryName(path);
            string fileName = Path.GetFileName(path);

            string sql = @"SELECT * FROM [" + fileName + "]";

            using (OleDbConnection connection = new OleDbConnection(
                      @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly +
                      ";Extended Properties=\"Text;HDR=" + header + "\""))
            using (OleDbCommand command = new OleDbCommand(sql, connection))
            using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
            {
                DataTable dataTable = new DataTable();
                dataTable.Locale = CultureInfo.CurrentCulture;
                adapter.Fill(dataTable);
                return dataTable;
            }
        }


    }
}



It works well with alphanumeric data
Like If we have below CSV data
Member1,BL,Yes
Member2,BL,No
Member3,BL,Yes

Then GetDataTableFromCsv retuen proper data in table like

Member1BLYes
Member2BLNo
Member3BLYes


ButIf we have below CSV data
001,BL,Yes
002,BL,No
003,BL,Yes

Then GetDataTableFromCsv does not return proper data in table like

1BLYes
2BLNo
3BLYes



I want return data to be in below format

001BLYes
002BLNo
003BLYes


In summary I want the above CSV parsing method consider memberid column as string datatype instead of numeric data type so that It will parse it as it is (with 00)


Thanks in Advance
--Rahul D.
Posted
Updated 19-Feb-12 22:10pm

This CSV article is pretty awesome[^] and is released under MIT licence.
 
Share this answer
 
Comments
RDBurmon 20-Feb-12 6:24am    
No , This require LumenWorks.Framework.IO.Csv; I don't want to use third party software anymore.
BobJanova 20-Feb-12 13:30pm    
Well, you should have specified that, because using a third party library is by far the best way to approach CSVs. The Jet ODBC driver is a third party library, anyway.
If you want to roll your own, you can do something like
string[][] result;

string[] lines = content.Split('\n');
result = new string[lines.Length][];
for(int ri = 0; ri < lines.Length; ri++)
 result[ri] = lines[ri].Split(',');


Be aware, though, that this does not implement the complete CSV standard. Handling quoted items, items with commas in etc is considerably trickier (which is why there are third party libraries available that do it for you).

I'm sure there is also a regex you could look up to do it. Regexes are typically quite inefficient, though (but if your tables are really 3×3 then that won't matter).
 
Share this answer
 
v2
Take a look at this page[^], it may have the solution you need.
 
Share this answer
 
Comments
RDBurmon 20-Feb-12 5:06am    
Have you read my Q clearly ? I said I know the process of CSV parsing. and I had issue while parsing the CSV file using the same method describe above link.
Richard MacCutchan 20-Feb-12 5:17am    
No, you did not say that at all, and if you look properly at the link I gave you it has a suggestion for parsing the data from CSV as text rather than numeric. Maybe you should at least try it to see what results you get.

Andreas Gieriet 21-Feb-12 5:25am    
My 5! It's the approach I would take - no matter what (unclear) objections the OP brings up...
Richard MacCutchan 21-Feb-12 5:30am    
Thanks; as always OP is free to do whatever he decides; whether that is a good or bad thing only time will tell.
Use the PadLeft method[^] in C# and put in 0's to the left.
 
Share this answer
 
Comments
RDBurmon 20-Feb-12 4:15am    
Link says
[C#] No example is available for C#. To view a Visual Basic, C++, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.
Abhinav S 20-Feb-12 4:18am    
You can use the syntax from the above link and also try http://www.dotnetperls.com/padleft for an example.
RDBurmon 20-Feb-12 4:23am    
But the thing is it may have below type of data
001,BL,Yes
2,BL,No
003,BL,Yes


And in that I want
001 BL Yes
2 BL No
003 BL Yes


RDBurmon 20-Feb-12 6:24am    
Abhinav , Any input ?
Since we can not handle by changing only CSV parsing method only I have made the changes in method as well as CSV formation process

Not instead of passing below CSV
001,BL,Yes
2,BL,No
003,BL,Yes

I am now passing this CSV
MID001,BL,Yes
MID2,BL,No
MID003,BL,Yes

And on C# side I will waved off "MID" and display character after that on Gridview and used that gridview data here after.
 
Share this answer
 
Comments
Andreas Gieriet 21-Feb-12 5:29am    
I regard this rather a "hack" than a solution. It works, but it's a brute-force way to enforce text format. If you want to control the format, you might go the approach suggested in Solution #2. Just my 5 cents.
BobJanova 21-Feb-12 6:30am    
'Since we can not handle by changing only CSV parsing method only' - this is false, I've given you two working solutions (3 and 4) for the problem as stated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900