Click here to Skip to main content
15,877,145 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my Porject, I have local Database which is crearted by adding new item to my windows form application (Service-base-Database)and within server explorer I built my own columns for example (ID,X,Y,Z).

The idea of project is I want to read text or csv file and display my data into GridView and save all values into my Local Database (*.mdf). Currently I am looking for a function or a way how to store my data into my local databse.

Of course, I searched a lot about this issue but I found nothing similar to my issue, I found how to insert data into sql server by using sqlBULKcopy which does not work in my case.

Any Idea?

What I have tried:

<pre>public void ImportData()
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Import text file";
            ofd.InitialDirectory = @"C:\";
            ofd.Multiselect = true;
            ofd.Filter = "txt file (*.txt)|*.txt| all files (*.*)|*.*";

                if (ofd.ShowDialog() == DialogResult.OK)
                    path = ofd.FileName;


                string[] lines = File.ReadAllLines(ofd.FileName);  // read all lines 
                string[] value;     // contents
              //  int count = 0;
                for (int i = 0; i < lines.Length; i++)
                    char c = ',';
                    value = lines[i].ToString().Split(c);    //    -  -  - 
                                                             //    -  -  -
                    string[] rows = new string[value.Length];

                    for (int j = 0; j < value.Length; j++)
                        rows[j] = value[j].Trim();
                       // listBox1.Items.Add(rows[j]);
                       // listBox1.Items.Add(value.Length);
                      //  listBox1.Items.Add(lines.Length);
                    MessageBox.Show("The data has been successfully read");
            catch (Exception ex)

Above code just to read my text file and get DataTable (dt)

Now I want to insert dt into my local database and save

I tried this code

tabledatagridview.datasource =dt; 
Updated 25-Nov-19 21:03pm

1 solution

CSV doesn't necessarily have to be just "separated by commas" - CSV data can also contain double quotes and newlines:
"Mike Jones", "2, Brain Drive, Fullchester"

Rather than "Brewing your own" CSV reader, try this: A Fast CSV Reader[^] - it does all the donkey work for you!
It can return a DataTable which you can either send directly to your DB via a DataAdapter or edit to produce just the data you want before sending it to the DB: OdbcDataAdapter Class (System.Data.Odbc) | Microsoft Docs[^]
Share this answer
EngAb1989 26-Nov-19 7:16am    
Thanks so much. I appreciate your hints.

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