Shashikant Verma

Shashikant Verma

  • NA
  • 173
  • 3.1k

Insert in database from excel sheet 2010 in ASP.net C#

Nov 30 2017 1:23 PM

 I have tried this code 

String strConnection = "Data Source=.;Initial Catalog=InsertFromExcel;User ID=sa";
string path = fileuploadExcel.PostedFile.FileName;
string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;Persist Security Info=False";
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
OleDbCommand cmd = new OleDbCommand("Select [ID],[Name],[Designation] from [Sheet1$]", excelConnection);
excelConnection.Open();
OleDbDataReader dReader;
dReader = cmd.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
//Give your Destination table name
sqlBulk.DestinationTableName = "Excel_table";
sqlBulk.WriteToServer(dReader);
excelConnection.Close();
 
 Generate an error 
  
The Microsoft Access database engine cannot open or write to the file ''. It is already opened exclusively by another user, or you need permission to view and write its data
 
please give a proper solutions.
 
Thanks 
 
 

Answers (1)