I am importing data from excel to database and I am successful in doing that. Is there any way where I can check if the current row exist in database before inserting that particular row from excel. Here is the code which I am using to import data from excel
con.Open(); if (FileUpload1.HasFile) { string path = FileUpload1.PostedFile.FileName; string saveFolder = @"E:\"+""+path+""; //Pick a folder on your machine to store the uploaded files FileUpload1.SaveAs(saveFolder); String excelConnString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + saveFolder + ";Extended Properties=Excel 12.0;"); OleDbConnection excelConnection = new OleDbConnection(excelConnString); OleDbCommand cmd = new OleDbCommand("Select * from [Sheet1$]", excelConnection); excelConnection.Open(); OleDbDataReader dReader; dReader = cmd.ExecuteReader(); SqlBulkCopy sqlBulk = new SqlBulkCopy(con); sqlBulk.DestinationTableName = "Course_Data"; sqlBulk.WriteToServer(dReader); excelConnection.Close(); con.Close(); Response.Write(" alert('File Uploaded Successfully');"); } else { Response.Write(" alert('First select the file which you need to upload.');"); } con.Close();
Thank You.
Aucun commentaire:
Enregistrer un commentaire