简体   繁体   中英

export data from CSV to datatable in c#

I am using below code to export data from a csv file to datatable. As the values are of mixed text ie both numbers and Alphabets, some of the columns are not getting exported to Datatable.

I have done some research here and found that we need to set ImportMixedType = Text and TypeGuessRows = 0 in registry which even did not solve the problem. Below code is working for some files even with mixed text.

Could someone tell me what is wrong with below code. Do I miss some thing here.

if (isFirstRowHeader)
{
    header = "Yes";
}

using (OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly +
                    ";Extended Properties=\"text;HDR=" + header + ";FMT=Delimited\";"))
{
    using (OleDbCommand command = new OleDbCommand(sql, connection))
    {                       
        using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
        {                           
            adapter.Fill(table);
        }
        connection.Close();
     }
 }

The main thing that would probably help is to first stop using OleDB objects for reading a delimited file. I suggest using the 'TextFieldParser' which is what I have successfully used for over 2 years now for a client.

http://www.dotnetperls.com/textfieldparser

There may be other issues, but without seeing your .CSV file, I can't tell you where your problem may lie.

The TextFieldParser is specifically designed to parse comma delimited files. The OleDb objects are not. So, start there and then we can determine what the problem may be, if it persists.

If you look at an example on the link I provided, they are merely writing lines to the console. You can alter this code portion to add rows to a DataTable object, as I do, for sorting purposes.

for comma delimited file this worked for me

public DataTable CSVtoDataTable(string inputpath)
   {

    DataTable csvdt = new DataTable();
    string Fulltext;
    if (File.Exists(inputpath))
    {
       using (StreamReader sr = new StreamReader(inputpath))
        {
            while (!sr.EndOfStream)
            {
                Fulltext = sr.ReadToEnd().ToString();//read full content
                string[] rows = Fulltext.Split('\n');//split file content to get the rows
                for (int i = 0; i < rows.Count() - 1; i++)
                {
                    var regex = new Regex("\\\"(.*?)\\\"");
                    var output = regex.Replace(rows[i], m => m.Value.Replace(",", "\\c"));//replace commas inside quotes
                    string[] rowValues = output.Split(',');//split rows with comma',' to get the column values
                    {
                        if (i == 0)
                        {
                            for (int j = 0; j < rowValues.Count(); j++)
                            {
                                csvdt.Columns.Add(rowValues[j].Replace("\\c",","));//headers
                            }

                        }
                        else
                        {
                            try
                            {
                                DataRow dr = csvdt.NewRow();
                                for (int k = 0; k < rowValues.Count(); k++)
                                {
                                    if (k >= dr.Table.Columns.Count)// more columns may exist
                                    { csvdt .Columns.Add("clmn" + k);
                                        dr = csvdt .NewRow();
                                    }
                                    dr[k] = rowValues[k].Replace("\\c", ",");

                                }
                                csvdt.Rows.Add(dr);//add other rows
                            }
                            catch
                            {
                                Console.WriteLine("error");
                            }
                        }
                    }
                }
            }
        }
    }
    return csvdt;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM