Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi developer's
I have a great problem to load a csv file in datatable . A reason is csv file values are seprated by (;). and when I given a (;) in oledb or odbc delimiter they can not identified them and given a "Index 121 type error", so then i am create schema.ini file and save them in bin\debug folder of the project . but after that again csv file not load in datatable.
I also tell u that i m load a many csv file that have a diffrent name then. is it possible to not add a file name in schema.ini file.

my code of project is please read this
C#
public static DataTable ParseCSV(string path)
       {

           if (!File.Exists(path))
               return null;

           string full = Path.GetFullPath(path);
           string file = Path.GetFileName(full);
           string dir = Path.GetDirectoryName(full);

           //create the "database" connection string
           string connString = "Provider=Microsoft.Jet.OLEDB.4.0;"
             + "Data Source=\"" + dir + "\\\";"
             + "Extended Properties=\"text;HDR=yes;FMT=Delimited\";";

           //create the database query
           string query = "SELECT * FROM " + file;

           //create a DataTable to hold the query results
           DataTable dTable = new DataTable();

           //create an OleDbDataAdapter to execute the query
           OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);

           try
           {
               //fill the DataTable
               dAdapter.Fill(dTable);
           }
           catch (System.Exception ex)
           {
               MessageBox.Show(ex.Message);
           }

           dAdapter.Dispose();

           return dTable;
       }

       private void button1_Click(object sender, EventArgs e)
       {
           try
           {
               DialogResult result = this.openFileDialog1.ShowDialog();
               if (result == DialogResult.OK)
               {
                   string filename = openFileDialog1.FileName;
                   textBox1.Text = filename;
               }
               else
               {
                   MessageBox.Show("dear user please select path");
               }
               DataTable dt = Form1.ParseCSV(textBox1.Text);

               dataGridView1.DataSource = dt.DefaultView;
           }
           catch (System.Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }

after write this code I m write a schema.ini file code
[file] // this is a vaariable name that is add in a datatable code
Format=delimited(;)

Please solve my problem that how load a csv(;)seprated file in dataset or a datable?

I always thankfull to you, please solve my problem.
Posted
Updated 3-Dec-10 5:45am
v2

If you don't find a way to resolve your problem, you might take a look at some of the many CSV parsing libraries at CodeProject. To list a few:

-CsvReader[^]
-GenericParser[^]
-Search in CodeProject...[^]
 
Share this answer
 
From what I've seen, I think that this:
C#
//create the "database" connection string
           string connString = "Provider=Microsoft.Jet.OLEDB.4.0;"
             + "Data Source=\"" + dir + "\\\";"
             + "Extended Properties=\"text;HDR=yes;FMT=Delimited\";";

should be this:
C#
//create the "database" connection string
           string connString = "Provider=Microsoft.Jet.OLEDB.4.0;"
             + "Data Source=" + dir + "\\;"
             + "Extended Properties=\"text;HDR=Yes;FMT=Delimited(;)\"";
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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