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
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(',');
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");
}
}