I am importing data from Excel into a SQL Server 2008 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
<pre lang="c#">
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("<Script> alert('File Uploaded Successfully');</Script>");
}
else
{
Response.Write("<Script> alert('First select the file which you need to upload.');</Script>");
}
con.Close();
</pre>
Thank You.