Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to import CSV file in to SQLCE database table, it is reading the Header Row also so please spme one help me to code........

This is my ".CSV" file format for Import

"Sr.No","Sales Order No","Shipment No.","Cust. Name","Item","Bundle No.","Roll No.","Qty","Trans Id","Validated By","Record Id"
1,"S0000093","GNL-000099","Aarpee Hygiene Pvt Ltd","H0010AW0101-01P-0600-06050","G1150505045","G1150504070406",14,"GNL-005111","",35637153767
2,"S0000093","GNL-000099","Aarpee Hygiene Pvt Ltd","H0010AW0101-01P-0600-06050","G1150505002","G1150504050301",46,"GNL-005111","",35637153768


This is the file After Exporting from SQLCE

Sr.No,Sales Order No,Shipment No.,Cust. Name,Item,Bundle No.,Roll No.,Qty,Trans Id,Validated By,Record Id
"Sr.No","Sales Order No","Shipment No.","Cust. Name","Item","Bundle No.","Roll No.","Qty","Trans Id","Validated By","Record Id"
1,"S0000093","GNL-000099","Aarpee Hygiene Pvt Ltd","H0010AW0101-01P-0600-06050","G1150505045","G1150504070406",14,"GNL-005111","",35637153767
2,"S0000093","GNL-000099","Aarpee Hygiene Pvt Ltd","H0010AW0101-01P-0600-06050","G1150505002","G1150504050301",46,"GNL-005111","",35637153768

this is my code for importing .CSV File

C#
private void SycItems()
        {
            string Path = null;
            string Line = null;
            string FileName = null;
            StreamReader Reader = default(StreamReader);
            Path = "\\My Documents";
            try
            {
                ConStr = "Data Source =" + System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\freedom.sdf";
                con.ConnectionString = ConStr;
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                Cmd.Connection = con;

                try
                {
                    if (File.Exists(Path + "\\Items.csv"))
                    {
                        FileName = "Items.txt";
                        Cmd.CommandText = "Delete from Items";
                        Cmd.ExecuteNonQuery();
                        FileInfo ItemsFile = new FileInfo(Path + "\\Items.csv");
                        Reader = ItemsFile.OpenText();
                        do
                        {

                            Line = Reader.ReadLine();
                            if (!(string.IsNullOrEmpty(Line)))
                            {
                                
                                string[] values = Line.Split(',');
                                //Edit column name
                                Cmd.CommandText = "Insert Into Items (No,Salesorder,Shipmentno,Custname,Itemno,Bundleno,Rollno,Qty,Transid,Validatedby,RecordID)values('" + values[0] + "','" + values[1] + "','" + values[2] + "','" + values[3] + "','" + values[4] + "','" + values[5] + "','" + values[6] + "','" + values[7] + "','" + values[8] + "','" + values[9] + "','" + values[10].Replace('\'', ' ') + "')";
                                //Cmd.CommandText = "Insert Into Items (No,Salesorder,Shipmentno,Custname,Itemno,Bundleno,Rollno,Qty,Transid,Validatedby,RecordID)values('" + values[0] + "','" + values[1] + "','" + values[2] + "','" + values[3] + "','" + values[4] + "','" + values[5] + "','" + values[6] + "','" + values[7] + "','" + values[8] + "','" + values[9] + "','" + values[10].Replace('\'',' ' ) + "')";
                                Cmd.ExecuteNonQuery();
                            }
                        } while (!(Line == null));
                        Reader.Close();

                        if (File.Exists(Path + "\\Items.csv"))
                        {
                            File.Delete(Path + "\\Items.csv");
                        }
                        MessageBox.Show("CSV Imported Successfuly", "Import CSV");
                    }
                    else
                    {
                        MessageBox.Show("Items File is not found", "Import CSV");
                        Application.Exit();
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Error in Items File", "Import CSV");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Error", "Import CSV");
            }
}
Posted
Updated 1-Jul-15 11:26am
v3
Comments
[no name] 1-Jul-15 17:26pm    
And the problem or question is... ?
Maciej Los 1-Jul-15 17:28pm    
:laugh:
My virtual 5!
Member 11602830 1-Jul-15 17:30pm    
i don't want to read .CSV file first row while importing in SQLCe table
[no name] 1-Jul-15 17:56pm    
Then don't read it or skip it. How is that a problem?

1 solution

OK, so read the first line of the file and don't do anything with it.

StreamReader.ReadLine()[^]
 
Share this answer
 
Comments
Maciej Los 1-Jul-15 17:58pm    
Really, is it enough? :laugh:
+5!

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